【问题标题】:Is there a way to use a Modal in a React Class Component有没有办法在 React 类组件中使用 Modal
【发布时间】:2021-03-19 09:48:30
【问题描述】:

单击模态框后,我正在尝试呈现单个草稿,而不是导航到其他组件...如果这有意义...我要显示它,但是由于链接,它会重定向我。但是如果我不添加链接,那么我会在 url 上丢失我的 draftId

这是我的代码的一部分

class Drafts extends React.Component {
  componentDidMount() {
    this.props.fetchAllDrafts()
  }

  render() {
    const drafts = this.props.drafts

    return (
      <React.Fragment>
        <Table aria-label="a dense table" className="myaccount-table">
          <TableHead>
            <TableRow>
              <TableCell align="center">
                <div className="date-content">Date</div>
              </TableCell>
              <TableCell align="center">Content</TableCell>
              <TableCell> </TableCell>
              <TableCell> </TableCell>
            </TableRow>
          </TableHead>
          <TableBody>
            {drafts.map((draft) => (
              <TableRow key={draft.id}>
                <TableCell>
                  <div className="datecontent">{draft.createdAt}</div>
                </TableCell>
                <TableCell>
                  <div className="content">{draft.content} </div>
                </TableCell>
                <TableCell>
                  <Link to={`/drafts/${draft.id}`}>
                    <Popup
                      trigger={<button className="button"> Read </button>}
                      modal
                      nested
                    >
                      <SingleDraft />
                      {/* <Button color="primary">
                        Read
                      </Button> */}
                    </Popup>
                  </Link>

来自另一个类组件

class SingleDraft extends React.Component {
  constructor() {
    super()
  }

  componentDidMount() {
    this.props.fetchDraft(this.props.match.params.draftId)
  }

  render() {
    return (
      <div>
        <p>{this.props.singleDraft.content} </p>
        <Link to={`/texteditor/${this.props.singleDraft.id}`}>
          <button>Update</button>
        </Link>
      </div>
    )
  }

【问题讨论】:

  • 请添加您尝试过的内容以及遇到的问题。

标签: node.js reactjs react-redux react-router material-ui


【解决方案1】:

为什么不将draftId 作为道具传递?

<Popup
    trigger={<button className="button"> Read </button>}
    modal
    nested
>
    <SingleDraft draftId={draft.id} />
    {/* <Button color="primary">Read</Button> */}
</Popup>

在你的组件中。

componentDidMount() {
    this.props.fetchDraft(this.props.draftId)
}

【讨论】:

  • 谢谢!试过了但没用
  • 您需要删除链接组件,以免它重定向您。
猜你喜欢
  • 2018-07-03
  • 2019-09-11
  • 2022-09-23
  • 1970-01-01
  • 2019-08-07
  • 2023-03-29
  • 2023-02-24
  • 1970-01-01
  • 2020-02-17
相关资源
最近更新 更多