react.js中的setState之后不呈现组件。
我正在尝试根据特定状态有条件地渲染组件。下面的代码位于另一个文件上的映射中。我有一个打开的设定台,每当用户单击“注释”按钮时,它应该将OpenState设置为true。但是,当我单击注释按钮时,它不会打开模式。模态通过open = {open}
的道具,我仍然没有打开它。
这是我想知道的代码
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>
</>
)
}
是否需要传递另一个组件内的道具,并在此文件中传递它。我的&lt; handlenotemodal /&gt; < /code>组件是在另一个文件上创建的,然后导入。
为了安全保持和时间,这是我的HandlenoteModal组件的代码。
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>
)
} ```
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>
)
} ```
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为对于你的情况,你不需要传递
open
属性来验证打开或关闭该模式组件你只需对你的上部组件进行条件检查,如下所示
这是它在你的中的样子代码
I think for your case, you don't need to pass
open
prop to validate opening or closing that modal componentYou just put a conditional check on your upper component like below
Here is how it looks like in your code