【问题标题】:React-redux REST API this.state is nullReact-redux REST API this.state 为 null
【发布时间】:2017-06-18 12:40:21
【问题描述】:

我是 Redux 的新手,目前正在使用 API 来获取数据。我正在尝试使用 React.cloneElement 将状态从我的父母传递给 this.props.children。我想我在使用 React.cloneElement 时犯了一个错误,因为当我将它传递给 cloneElement 函数时,调试器显示状态为空。以下是我的父渲染方法:

render(){

    const {courses} = this.state;
    debugger;
    let fn = (child) => {
        return React.cloneElement(child, {
            courses: courses
        });
    };

    let childrenWithProps = React.Children.map(this.props.children, fn);

    return (
        <div>
            <div className="container jumbotron jumbotron-fluid">
                <h1>CoursesPage</h1>
                <p>This page adds and lists all the courses</p>
                <Link to="/courses/courseslist">
                    <Button color="primary">Course Listing</Button>
                </Link>
            </div>
            {childrenWithProps}
        </div>

    );
}

从控制台,我可以公平地假设它正确地调用了孩子,但在课程中传递了 null 值。但是,当我简单地通过 &lt;CourseList courses={courses} /&gt; 时,它会正确地假定该状态。那么我到底哪里错了?

我在控制台中收到以下错误:

Uncaught TypeError: Cannot read property 'map' of undefined
at CourseList (courseList.js:20)
at ReactCompositeComponent.js:305
at measureLifeCyclePerf (ReactCompositeComponent.js:75)
at ReactCompositeComponentWrapper._constructComponentWithoutOwner (ReactCompositeComponent.js:304)
at ReactCompositeComponentWrapper._constructComponent (ReactCompositeComponent.js:279)
at ReactCompositeComponentWrapper.mountComponent (ReactCompositeComponent.js:187)
at Object.mountComponent (ReactReconciler.js:45)
at ReactDOMComponent.mountChildren (ReactMultiChild.js:236)
at ReactDOMComponent._createInitialChildren (ReactDOMComponent.js:703)
at ReactDOMComponent.mountComponent (ReactDOMComponent.js:522)

..其中 courseList 是子组件。

非常感谢您的帮助。

【问题讨论】:

    标签: javascript reactjs ecmascript-6 react-redux


    【解决方案1】:

    由于您要将变量从父类传递给子类CourseList,因此您需要改用props

    const {courses} = this.props;
    

    文档链接Components and Props

    这可能是你想要的。

    render(){
        const {coursesList} = this.props;
    
        return (
            <div>
                <div className="container jumbotron jumbotron-fluid">
                    <h1>CoursesPage</h1>
                    <p>This page adds and lists all the courses</p>
                    <Link to="/courses/courseslist">
                        <Button color="primary">Course Listing</Button>
                    </Link>
                </div>
                {coursesList}
            </div>
    
        );
    }
    

    【讨论】:

    • @X PLOT1ON 确实有效,但我没有从文档中清楚地了解何时提交 this.state 与 this.props。另外,如果我的意图是克隆状态或将状态“传递”给我的子组件,那么 this.props 是如何在这里发挥作用的?
    • 也许这可能是区分 props 和 state 的更好方法stackoverflow.com/a/27992380/4540216 简单来说,props 是子组件接受的对象,并且 state 在组件内是可变的,可以触发 re-渲染
    • 我刚刚编辑了您的代码(并粘贴在我的答案中),这可能是您想要的,而不是复制对象。 @Omkar
    猜你喜欢
    • 2016-05-20
    • 2016-06-20
    • 1970-01-01
    • 2014-12-12
    • 1970-01-01
    • 1970-01-01
    • 2020-07-11
    • 2019-06-06
    • 2018-07-29
    相关资源
    最近更新 更多