简体   繁体   English

反应:mapStateToProps - 我如何获得 state?

[英]React: mapStateToProps - How do I get state?

I use mapStateToProps, mapDispatchToProps to pass function and state to myComponent, I can get function but can't get state. I use mapStateToProps, mapDispatchToProps to pass function and state to myComponent, I can get function but can't get state. My code looks like that:我的代码如下所示:

const mapStateToProps = (state) => ({
  ...state.editor
});

const mapDispatchToProps = (dispatch) => ({
  onLoad: (payload) => dispatch(actionCreators.myFunc(payload)),}
});

function myComponent({errors, match, onLoad}) {
  //function myComponent({errors, match, onLoad, state}) { //state undefined
  //function myComponent({errors, match, onLoad}, state) { //state undefined
  // How do I get state here?
  console.log(state) //always undefined
  console.log(state.editor) //undefined
   console.log(editor) //undefined
}

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

Change your mapStateToProps like below如下更改您的mapStateToProps

const mapStateToProps = (state) => ({
  editor:state.editor
});

Typically example for Functional component is Functional component的典型示例是

function YourComponent({ yourProp }) {
  return <p>{yourProp}</p>
}

function mapStateToProps(state) {
  return { yourProp: state.yourProp };
} 

export default connect(mapStateToProps)(YourComponent);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM