When I check the length of item, and trying to add in to template, getting error. any one help me to fix this issue?
here is my code :
import React from "react";
import "./style-posts.css";
import { connect } from "react-redux";
import { setTitle, titleHandler } from "../actions/postActions";
const ShowPosts = props => {
if (props.posts.length) {
const postItems = props.posts.map(post => {
<div key={post.id}>
<h3>{post.title}</h3>
<p>{post.body}</p>
</div>;
});
}
return (
<div>
{props.posts.length ? postItems : null} //postItems is not defined
<button onClick={props.showPosts}>Show Posts On Click </button>
</div>
);
};
export default connect(
setTitle,
titleHandler
)(ShowPosts);
getting error as postItems is not defined
- what is wrong with me code? any one figure out?