ReduxSimpleStarter icon indicating copy to clipboard operation
ReduxSimpleStarter copied to clipboard

MapStateToProps

Open martinc360 opened this issue 7 years ago • 1 comments

I have a small problem with mapStateToProps and cant find the sollutions.

The code below works, but i want to use mapStateToProps correctly as described in your videos.

If I change the line "post: posts" to "post: posts[ownProps.match.params.id]" and changed the render method from "{post[this.props.match.params.id].title}" to "{post.title}" I got the following message:

Uncaught TypeError: Cannot read property '242339' of null

Thanks in advance!

`class PostsShow extends Component { componentDidMount() { const { id } = this.props.match.params; this.props.fetchPost(id); }

render() { const { post } = this.props; console.log(this.props.post);

if (!post ) {
  return (<div>Loading...</div>);
}

return (
  <div>
    {post[this.props.match.params.id].title}
  </div>
);

} }

const mapStateToProps = ({posts}, ownProps) => { console.log(ownProps);

return { post: posts }; };`

martinc360 avatar Jun 01 '18 14:06 martinc360

Do you have a repo with your code? I'd like to check it out and help you.

David2913 avatar Jun 21 '18 16:06 David2913