简体   繁体   English

ReactJS:如何在另一个功能组件中读取链接 react-router 中传递的值

[英]ReactJS : How to read value passed in Link react-router in another functional component

I have followed the suggestion on Pass props in Link react-router我遵循了链接 react-router 中关于通行证道具的建议

I'm sending Link value to the ReactJS functional component and while accessing it returning empty, I'm sure missing something basic here.我正在将Link值发送到 ReactJS功能组件,并且在访问它时返回空值,我确定这里遗漏了一些基本的东西。

This is my Route path这是我的路线路径

            <Route path="/report/:id">
                <ReportPage />
            </Route>

The report page which is trying to get value from either query param or state param both returns empty试图从查询参数或 state 参数中获取值的报告页面都返回空

const ReportPage = (props) => {
    const { query } = useParams();
    const { hello } = useState()

    console.log(query) // Return Empty for Try #2 
    console.log(hello) // Return Empty for Try #1
    console.log(this.props.match.params.hello) // Undefined for 

    return (
        <DashboardLayout>
            <h2> AMI Test Reports </h2>
        </DashboardLayout>
    )
}
                

DashbaordPage which is Linking正在链接的 DashbaordPage

Tried 1 Using the state尝试 1使用 state

                   <Link
                    to={{
                        pathname: "/report/1",
                        state: {
                            hello: "Hello World"
                        }
                    }}>
                    Press Me
                </Link>

Tried #2 using the query使用查询尝试#2

                   <Link
                    to={{
                        pathname: "/report/1",
                        query: "test"
                    }}>
                    Press Me
                </Link>

What is the right way to access the Value passed in the Link in another functional component.在另一个功能组件中访问链接中传递的值的正确方法是什么。

useParams returns an object containing all of the available parameters. useParams 返回一个包含所有可用参数的 object。 What you currently have is useParams returning an object that contains a params property which is incorrect.您当前拥有的是 useParams 返回一个 object,其中包含一个不正确的params属性。

what you want:你想要什么:

const { id } = useParams();

As for the location state, you'd get that from useLocation, not useState.至于位置 state,您将从 useLocation 而不是 useState 获得它。

const { state } = useLocation()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过 React Router Link 将值从功能组件传递到另一个功能组件? - How can I pass values from a functional component through a React Router Link to another functional component? 如何从中获取传递数据的值<Link/>我在 Reactjs 中的下一个功能组件页面中的组件 - How to get the value of the passed data from <Link/> component in my next functional component page in Reactjs React-Router链接,如何触发来自另一个组件的链接上的点击事件 - React-Router Link, how to trigger a click event on a Link from another component ReactJS:React-Router 将值传递到另一个页面 - ReactJS: React-Router pass value to another page ReactJS + React Router:如何禁用来自React-Router的链接 <Link> ? - ReactJS + React Router: How to disable link from React-Router's <Link>? 如何使用ReactJS将props传递到react-router(和react-router) - How to pass props into a react-router with ReactJS (and react-router) 如何用 React-Router 中的历史记录替换链接组件行为 - How to replace Link component behavior with History in React-Router 如何从文本呈现 react-router Link 组件? - How to render a react-router Link component from text? 如何在每个组件中具有“链接”的反应路由器中使用“CSSTransition”? - How to use 'CSSTransition' in react-router that has 'Link' in each component? 如何用react-router替换react组件 - How to replace a react component with react-router
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM