首页 > 解决方案 > 将 props 从父组件传递给子组件并以 redux 形式使用 connect

问题描述

TypeError:无法读取未定义的属性“状态”

但是在父组件上,我将状态作为道具传递。为什么我无法在这里获得 this.props.status ?

 class App extends Component{
   render(){
      return (
       <div>
         <childComponent 
           status={true}
         />
       <div>

     )
    }
  }

ChildComponent.js

 class childComponent extends Component{
   render(){
    console.log("props passed", this.props.status) // getting true 
    return(

   )

  }
}


export default reduxForm({
  form: 'wizard', 
  destroyOnUnmount: false, 
  forceUnregisterOnUnmount: true,
  enableReinitialize: this.props.status ? true : false, // error happening
})(
  connect(
    mapStateToProps,
    mapDispatchToProps
  )(childComponent)
);

与此问题相关的问题

标签: reactjsredux-form

解决方案


您只能访问班级this.props内部childComponent。外部将无法访问它们。

如果您需要enableReinitialize动态更改参数或任何其他参数,也许此链接会对您有所帮助。 https://github.com/erikras/redux-form/issues/603#issuecomment-239543148


推荐阅读