I'm trying to render a component conditionally based on a particular state. The code below is inside of a mapping on another file. I have a setState const of open and whenever a user clicks the 'comment' button it should be setting the openState to true. However, when I click the comment button it does not open the modal. The modal is passed a prop of open={open} and i'm still not getting it to open.
here is the code
const { blog, idx, id } = props
const [open, setOpen] = useState({});
const handleOpen = () => setOpen(true);
const handleClose = () => setOpen(false);
return (
<>
<div className="blogBox" id={id} key={idx}>
<h4>{blog.title}</h4>
<hr />
<p>{blog.body}</p>
<hr />
<p id="postedBy">Posted By: Claude @ {blog.createdAt}</p>
<hr />
<button className="btn btn-info" onClick={handleOpen}>Comment</button>
<HandleNoteModal open={open} onClose={handleClose} />
<hr />
<div className="comment Section">
<span>User 1:</span>
<span> Comment one</span>
<br />
<span>User 2:</span>
<span> Comment two</span>
</div>
</div>
</>
)
}
I'm wondering if i need to be passing the prop inside the other component as well as passing it in here on this file. my <HandleNoteModal /> component was created on another file and then imported.
for safe keeping and time, here is the code for my HandleNoteModal component.
const { handleClose } = props
const [noteState, setNoteState] = useState({
user: '',
note: ''
});
const handleInputChange = ({ target: { name, value } }) => {
setNoteState({ ...noteState, [name]: value });
};
return (
<div className="modal" tabIndex="-1">
<div className="modal-dialog">
<div className="modal-content">
<div className="modal-header">
<h5 className="modal-title">Leave a Comment</h5>
<button type="button" className="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div className="modal-body">
<form>
<div className="mb-3">
<label htmlFor="note" className="form-label">Comment:</label>
<textarea name="note" className="form-control formBody" placeholder="Enter comment here" onChange={handleInputChange} required />
</div>
<button type="submit" className="btn btn-primary">Send</button>
</form>
</div>
<div className="modal-footer">
<button type="button" className="btn btn-secondary" data-bs-dismiss="modal" onClick={handleClose}>Close</button>
</div>
</div>
</div>
</div>
)
} ```
HandleNoteModalisopenprop, but seemingly you haven't shared it inHandleNoteModalcode. Please show how you integrateopenprop in that component