It’s giving error state.users.find is not a function but I have passed mapStateToProps as argument in the connect function

import React from ‘react’ import { connect } from "react-redux"; import { fetchUser }from "../actions" … Read more It’s giving error state.users.find is not a function but I have passed mapStateToProps as argument in the connect function

import React from 'react'

import { connect } from "react-redux";
import { fetchUser }from "../actions"

class UserHeader extends React.Component {

componentDidMount(){

    this.props.fetchUser(this.props.userId);
}
render() {

  const { user } = this.props;
     
    if(!user)
    {
        return <div>Loading..</div>;
    }

    return <div className="header">{user.name}</div>;
}

}

    const mapStateToProps = (state , ownProps) => {
return {user : state.users.find(user => user.id === ownProps.userId)}

}

 export default connect(mapStateToProps,{ fetchUser })(UserHeader);

Source: React – Stack Overflow


Categories: NewsTags:

Tags:


Leave a Reply

Your email address will not be published. Required fields are marked *