如何将参数道具传递给redux容器

时间:2020-01-22 22:40:51

标签: reactjs react-redux

我有一个选择器,需要用户的ID,理想情况下,它应来自params。

localhost:3001 / users / 1

选择器

export const getPosts = () => // this gets all posts
  createSelector(
    postSelector,
    (state) => state.posts,
 );

export const getUserPosts = (params) =>
    createSelector(
        postSelector,
        state => state.posts.filter(user => user.userId === params.userId)
    )

我该如何将参数传递给容器组件。

容器

import { connect } from "react-redux";
import { createStructuredSelector } from "reselect";
import Profile from "./../components/profile/profile"
import { getUserPosts } from "./../selectors/selectors";

const mapStateToProps = createStructuredSelector({
    posts: getUserPosts(), // how do i pass params within this selector
});

export default connect(
    mapStateToProps,
    null,
)(Profile);

考虑到容器逻辑与组件是分开的。

个人资料

import React, { Component, useState, useEffect } from 'react';
export interface IProps {
    match?: any
}
class Profile extends Component<IProps, {}> {
    render() {
        console.log(this.props.match.params) // this needs to be passed to container
        return (
            <div>
            </div>
        )
    }
}

export default Profile;

理想情况下,您可以将容器逻辑添加到Profile组件,但这看起来像是反模式,因为该组件后面总是跟随着一个容器。

2 个答案:

答案 0 :(得分:1)

首先,int作为一个函数可以像second argument那样访问这些道具,因为匹配对象是容器的道具,该容器向下传递到Nim。或者换句话说,没有static ,这很容易做到:

import java.lang.Math;
import java.util.Scanner;
import java.util.Random;

public class Nim {

    int numstones;
    Random rand;

    public Nim() {
        rand = new Random();
        numstones = 15 + rand.nextInt(16);
    }

    public void Usermove() {
        Scanner input = new Scanner(System.in);
        int takestones;
        System.out.println("How many would you like: ");
        takestones = input.nextInt();
        while ((TakeStones < 1) || (TakeStones > 3) || (TakeStones > NumStones)) {
            System.out.println("Value must be between 1 and 3");
            System.out.println("How many would you like? ");
            takestones = input.nextInt();
        }
        numstones -= takestones;
    }

    public void Computermove() {
        int takestones;
        do {
            takestones = 1 + rand.nextInt(3);
        }
        while ((TakeStones < 1) || (TakeStones > 3) || (TakeStones > NumStones));
        System.out.println("The computer takes " + Integer.toString(takestones) + ".");
        numstones -= takestones;
    }

    public void PlayNim() {
        while (true) {
            if (numstones > 0) {
                System.out.println("There are " + Integer.toString(numstones) + " stones");
                Usermove();
            }
            else {
                System.out.print("You Win!");
                break;
            }
            if (numstones > 0) {
                System.out.println("There are " + Integer.toString(numstones) + " stones");
                Computermove();
                break;
            }
        }
    }

    public static void main(String[] args) {
        Nim game = new Nim();
        game.PlayNim();
    }
}

关于mapStateToProps我还没有使用,但是我认为重新选择自述文件中的this部分会为您提供帮助。

如果我没看错,您需要做的就是编写选择器,使选择器以Profile作为第二个参数。这些道具应该从reselect传递到选择器,就像const mapStateToProps = (state, ownProps) => { return { posts: state.posts.filter(post => post.userId === ownProps.match.params.userId) } } 参数一样。

不确定是否这样做会有所帮助:

reselect

也许props需要更改。


此外,作为备用,我认为您当前的代码可能会起作用:

mapStateToProps

答案 1 :(得分:0)

这是我能够实现的使其与选择器一起使用的逻辑。

const mapStateToProps = (state, ownProps) => createStructuredSelector({
    posts: getUserPosts(ownProps.match.params),
})