简体   繁体   中英

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. 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

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

Typically example for Functional component is

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

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

export default connect(mapStateToProps)(YourComponent);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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