代码之家  ›  专栏  ›  技术社区  ›  tatsu

如何将道具从容器传递到其组件

  •  0
  • tatsu  · 技术社区  · 9 年前

    所以我有一个变量,我可以在我的组件中提取,虽然我不能确定,因为没有地方可以放置控制台。登录组件,但我想我有我的var。

    这是在我的构造函数中:

    function mapStateToProps(state) {
        return {
            parameters: state.searchParameterStore.parameters,
            idUser: state.authenticate.idUser,
        };
    }
    
    function mapDispatchToProps(dispatch) {
        return {
            exportParameter: payload => dispatch(actions.setParameters(payload)),
            generateDoc: payload => dispatch(actions.generateDoc(payload)),
        };
    }
    

    这在我的组件中:

    const propTypes = {
        generateDoc: React.PropTypes.func,
        exportParameter: React.PropTypes.func,
        parameters: React.PropTypes.array,
        idUser: React.PropTypes.string,
    };
    
    class generateComponent extends Component {
    generate() {
            const truc = this.props.idUser;
            console.log(truc);
    }
    }
    SearchExtendedComponent.propTypes = propTypes;
    
    export default SearchExtendedComponent;
    

    未定义(在控制台日志所在的行上)

    1 回复  |  直到 9 年前
        1
  •  1
  •   JulienD    9 年前

    显然,您正在使用react-redux,需要连接您的组件:

    import { connect } from 'react-redux';
    
    export default connect(mapStateToProps, mapDispatchToProps)(SearchExtendedComponent);
    

    这是文档中的相关示例:

    https://github.com/reactjs/react-redux/blob/master/docs/api.md#inject-todos-and-all-action-creators-addtodo-completetodo--as-actions