如何在从一个页面重定向到另一个组件时将道具传递给另一个组件。
单击“视图按钮”时,我希望将details状态传递给另一个组件,即{分页},该组件将在新页上打开。
我正在尝试用url <Route path="/detilaing" exact component={PAGING} />打开新页面上的分页组件。
单击按钮,我只想要选项卡和单击按钮详细信息,并隐藏其他部分。
发布于 2020-05-12 13:27:01
有几件事你需要更新
export default function App() {
return (
<Router>
<Combined />
</Router>
);
}<Route path="/detilaing/:ability" exact component={PAGING} />useHistory来获取历史对象并调用history.push。记住,重定向只在呈现组件时起作用,而不是使用事件处理程序。 const handleDetails = name => {
setdetails(name);
if (name) {
return history.push(`/detilaing/${name}`);
} else {
return history.push(`/`);
}
};import React from "react";
export default function PAGING({ match }) {
return (
<div>
<p>{match.params.ability}</p>
</div>
);
}现在,假设您希望将多个字符串传递给分页组件,并且您不希望在刷新时将其持久化,则可以使用状态传递数据。
history.push({
pathname: '/detilaing',
state: { detail }
})并在page.js中使用它。
export default function PAGING({ location }) {
return (
<div>
<p>{location.state && location.state.detail}</p>
</div>
);
}发布于 2020-05-12 13:20:12
this.context.router.push({
pathname: '/detilaing',
state: {details: this.state.details}
})通过以下方式进入状态:
this.props.location.state.detailshttps://stackoverflow.com/questions/61752657
复制相似问题