【问题标题】:React - Passing state to another component - Component accessed via URLReact - 将状态传递给另一个组件 - 通过 URL 访问的组件
【发布时间】:2021-10-10 01:00:15
【问题描述】:

您好,我是 React 的新手(第 2 天)。我只是想将状态从一个组件传递到另一个组件,但我一直在努力寻找一个以我的项目结构方式处理这个问题的教程(我想我的项目结构非常糟糕......)。我在 codeSandbox 上上传了可重现的代码:codesandbox.io/s/dreamy-maxwell-q83lc

我的代码

我的App.js 目前看起来像这样。我通过不同的 url 访问组件屏幕。

import componentOne from "./componentOne"
import componentTwo from "./componentTwo"

function App() {
    return (
        <div>
            <Router>
                <Switch>

                    <Route path = "/componentOne">
                        <componentOne/>
                    </Route>

                    <Route path = "/componentTwo">
                        <componentTwo/>
                    </Route>

                </Switch>
            </Router>
        </div>
    )
}

export default App

我想将状态从componentOne 传递到componentTwo。这是我的componentOne.jsx 文件。通过单击图像,URL 会发生变化,因此我位于 componentTwo 页面。但我不确定如何以这种方式将状态从componentOne 传递给它。

import {useState, useEffect} from "react";

function componentOne() {
    const [stateOne, setStateOne] = useState(null)
    const [stateTwo, setStateTwo] = useState(null)
    const [stateThree, setStateThree] = useState(null)

    /**
     * API call that populates stateOne, stateTwo and stateThree
     */

    return (
        <div>
                        
            {/*I want to pass stateOne to componentTwo*/}
            <a href="/componentTwo">
                <img src = {stateOne.image} alt={""}/>
            </a>

            {/*I want to pass stateTwo to componentTwo*/}
            <a href="/componentTwo">
                <img src = {stateTwo.image} alt={""}/>
            </a>

            {/*I want to pass stateThree to componentTwo*/}
            <a href="/componentTwo">
                <img src = {stateThree.image} alt={""}/>
            </a>

        </div>
    )
}

export default componentOne

这是我的componentTwo.jsx 文件。请注意,我要传递给componentTwo 的数据是 JSON。

function componentTwo(props) {
    // I want my JSON data to be in props

    return(
        // I then want to use this JSON data to display things
        <text> {props.title} </text>
    )
}

export default componentTwo

到目前为止我已经尝试过什么

到目前为止,我已经从App.js 中删除了componentTwo 路由路径,并以这种方式将其添加到componentOne,但它似乎不起作用。

function componentOne() {
    return (
        <div>
        ...

           <Router>
                <Switch>
                    <Route path = "/componentTwo">
                        <componentTwo recipe={stateThree}/>
                    </Route>
                </Switch>
            </Router>
            <a href="/componentTwo">
                <img src = {stateThree.image} alt={""}/>
            </a>
        </div>
    )

}

【问题讨论】:

标签: reactjs react-hooks react-router


【解决方案1】:

我建议创建一个重定向函数,代码如下:

this.props.history.push({
  pathname: '/componentTwo',
  search: '',
  state: stateTwo
})

但在你的情况下,你应该用 Link 组件替换你的

<Link to={{
  pathname: '/componentTwo',
  search: '',
  state: stateTwo
}}> ... </Link>

然后在 ComponentTwo 中,您可以使用如下代码访问状态:

props.location.state.stateTwo

记得从 props 或使用 withRouter HOC 获取历史和位置对象。

【讨论】:

  • 感谢您的回复。当我单击图像时,网址正确更改为“.../componentTwo”,但页面本身没有更新。我必须手动刷新它。无论如何我可以自动刷新吗?您还可以进一步说明如何访问 ComponentTwo 中的状态吗?目前我有一个 function componentTwo(props)stateTwo 是一个 JSON。
  • 据我了解 stateTwo 是一个对象,因此您应该使用 props.location.state.stateTwo 获取它(我的答案有误,抱歉)。
  • 点击图片后检查控制台是否有错误
  • componentTwo 我不知道我是否会使用stateOne、stateTwo 或stateThree。对此我能做些什么吗?我还检查了控制台,单击图像后没有出现错误。
  • 如果你能设置一个codeSandbox就好了,我们可以更好地帮助你。
【解决方案2】:

您应该使用React Router,它具有可用于传递状态的Link 组件。

<Link to={{pathname: "/componentTwo", state: yourstate }}>
    <img src = {stateOne.image} alt=""/>
</Link>

【讨论】:

  • 当我这样做时,url 会改变,但页面实际上并没有导航到那里。这是我的沙箱:codesandbox.io/s/dreamy-maxwell-q83lc 非常感谢任何帮助。
猜你喜欢
  • 1970-01-01
  • 2017-11-07
  • 2016-12-05
  • 2019-07-17
  • 2019-05-30
  • 1970-01-01
  • 2021-01-11
  • 2019-06-14
  • 2023-01-07
相关资源
最近更新 更多