MapStateToProps
martinc360 opened this issue · 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
};
};`
Do you have a repo with your code? I'd like to check it out and help you.