【发布时间】:2021-06-30 07:31:00
【问题描述】:
我有一个组件,我使用useHistory 在单击按钮时移动到另一个组件。这是到目前为止的代码:
Component1.tsx
let history = useHistory();
const someEventHandler = event => {
history.push({
pathname: '/Comp2',
search: '?flow=abc',
state: { detail: 'some_value' }
});
};
...
<Button onClick={someEventHandler}>Submit</Button>
Component2.tsx
const location = useLocation();
useEffect(
() => {
console.log(location.pathname);
console.log((location.state).detail); <-- Error here
}
)
按钮正在使用正确的路径和 queryParams 进行重定向。但我无法获得从第一个组件发送的详细信息部分。
我收到以下错误:
any
Property 'details' does not exist on type 'unknown'.ts(2339)
我正确发送了道具,但仍然无法从useLocation 接收数据(详细信息)部分。我们能以某种方式得到这个吗?
【问题讨论】:
标签: reactjs typescript