Search code examples
reactjsreduxstore

Pass id from redux into state of component


Here is state of my component:

 state = {
          data: {
            userId: `${user.id}`,
            name: '',
            country: '',
            city: ''
        },
        loading: false,
        errors: {}
       };

And using redux I retrieve the user id from logged in user:

function mapStateToProps(state){
  return{
    user: state.user
  }
}
QuestForm.propTypes = {
  user:PropTypes.shape({
    email: PropTypes.string.isRequired
  }).isRequired,
  submit: PropTypes.func.isRequired
};

export default connect(mapStateToProps, null)(Form);

It complains to me that 'user' is not defined no-undef

Is there a problem with syntax within a state or with my redux logic approach? Thank you in advance!


Solution

  • ${user.id} should be ${props.user.id} or ${this.props.user.id} depending whether you have a class or function based component.

    user is not defined because that's not how you access props :)