2017-06-18 140 views
0

当我尝试加载我的组件首次,双亲状态是完全传递给孩子。每件事都看起来不错。但是,当我刷新我的页面时,我无法看到传递给子项的状态。我只是看到一些来自孩子的静态内容。以下是我的父母:刷新页面反应父状态不会传递到子状态

render(){ 

    const {courses} = this.props; 
    // 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> 

    ); 
} 
} 

CoursesPage.propTypes = { 
    courses: PropTypes.array.isRequired, 
    actions: PropTypes.object.isRequired, 
    children: PropTypes.object.isRequired 
}; 

function mapStateToProps(state, ownProps){ 
    return { 
     courses: state.courses 
    }; 
} 

//mapping all course actions to actions in the props 
function mapDispatchToProps(dispatch){ 
    return { 
     actions: bindActionCreators(courseActions, dispatch) 
    }; 
} 


export default connect(mapStateToProps, mapDispatchToProps)(CoursesPage); 

此外,这是我的孩子组成:

class CourseDetailsAndList extends React.Component { 
constructor(props, context){ 
     super(props, context); 
     this.state = { 
      courses: this.props.courses 
     }; 
    } 
render(){ 
    const {courses} = this.props; 
    return (
     <div> 
      <h1>Courses</h1> 
      <CourseList courses={this.state.courses}/> 
     </div> 
    ); 
    } 
} 

CourseDetailsAndList.propTypes = { 
    courses: PropTypes.array.isRequired 
}; 

当我刷新页面,我没有看到孩子顺利通过课程状态,只看到一个空白的孩子。在同一行,我看到这个警告在控制台,它当然看起来的原因之一:

Warning: Failed prop type: The prop `courses` is marked as required in `CourseDetailsAndList`, but its value is `undefined`. 
in CourseDetailsAndList (created by RouterContext) 
in RouterContext (created by Router) 
in Router 
in Provider 

是否有人可以告诉我,我错了我传递的状态到子组件?为什么警告?

+0

你已经有了''中CoursesPage.propTypes' courses'注释掉。 你确定你正在为'CoursesPage.courses'传递一个有效的值吗? – Scott

+0

@斯科特是的,我传递了正确的价值。按钮onclick事件首先正确拉取数据。当我刷新页面时,它只显示孩子而不显示数据。这意味着GET数据的操作不会触发。但我不明白我出错的地方。我是否也需要在子组件中使用mapDispatchToProps? – Omkar

+0

'mapDispatchToProps'是一个redux帮助器方法,因此在这个实例中是否使用该方法将取决于您是否在此应用中使用了redux。 在你上面分享的代码中,你只包含了'CoursesPage'组件的'render'方法。你能分享整个组件吗? – Scott

回答

1

这里是你要完成的工作的例子,使用组件的精简版本:

https://jsfiddle.net/79qk1r50/2/

class ParentComponent extends React.Component { 
    render() { 
    const {parentProp} = this.props; 
    const childrenWithProps = React.Children.map(this.props.children, function(child) { 
     return React.cloneElement(child, {parentProp: parentProp}); 
    }); 

    return (
     <div> 
     <h1>ParentComponent</h1> 
     My parentProp is: {parentProp} 
     {childrenWithProps} 
     </div> 
    ); 
    } 
} 

class ChildComponent extends React.Component { 
    render(){ 
    const {parentProp} = this.props; 
    return (
     <div> 
     <h2>Child Component</h2> 
     Received from parent: {parentProp} 
     </div> 
    ); 
    } 
} 

React.render(
    <ParentComponent parentProp={'foo'}> 
    <ChildComponent /> 
    <ChildComponent />  
    </ParentComponent>, 
    document.getElementById('container')); 
+0

嘿斯科特,这绝对有效,除了我使用react-router来渲染子组件,而不是在渲染函数中对它们进行硬编码。 – Omkar

相关问题