I am new using react.
I'd like to add REF in my component only if it is not null.
if (posts.length === index + 1) {
number = post.id;
return <Post key={post.id} id={post.id} ref={lastPostElementRef} titulo={post.titulo} />
}
else {
return <Post key={post.id} id={post.id} titulo={post.titulo} />
}
if it is the last post
it should add a ref on it. The others will not got this ref val.
so I try:
const Post = React.forwardRef((props, ref) => (
<div
className={classes.post}
//if (ref != null) ???
ref={ref} >
data-id: {props.id}
titulo: {props.titulo}
</div>
));
export default Post;
how can I check if ref is not null inside my JSX to add or not the ref={ref} value inside my div?