react.js中的setState之后不呈现组件。

发布于 2025-01-18 04:05:10 字数 2789 浏览 0 评论 0原文

我正在尝试根据特定状态有条件地渲染组件。下面的代码位于另一个文件上的映射中。我有一个打开的设定台,每当用户单击“注释”按钮时,它应该将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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟花肆意 2025-01-25 04:05:10

我认为对于你的情况,你不需要传递 open 属性来验证打开或关闭该模式组件

你只需对你的上部组件进行条件检查,如下所示

{open && <HandleNoteModal onClose={handleClose} />}

这是它在你的中的样子代码

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>
        {open && <HandleNoteModal 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 think for your case, you don't need to pass open prop to validate opening or closing that modal component

You just put a conditional check on your upper component like below

{open && <HandleNoteModal onClose={handleClose} />}

Here is how it looks like in your 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>
        {open && <HandleNoteModal 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>
    </>
  )

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文