我们如何在React-Router v4中通过this.props.history.push('/page')传递参数?
.then(response => {
var r = this;
if (response.status >= 200 && response.status < 300) {
r.props.history.push('/template');
});
我们如何在React-Router v4中通过this.props.history.push('/page')传递参数?
.then(response => {
var r = this;
if (response.status >= 200 && response.status < 300) {
r.props.history.push('/template');
});
当前回答
您可以使用location将状态发送到其他组件,就像这样
在源组件中
this.props.history.push(pathComponent, sendState);
pathComponent是接收状态的目标组件
在目标组件中 如果您使用类组件,您可以像这样接收状态
Javascript版本
constructor(props) {
this.state = this.props.location.state
}
打印稿版本
constructor(props: {}) {
const receiveState = this.props.location.state as StateType // you must parse into your state interface or type
this.state = receiveState
}
奖金
如果你想重置收到的状态。使用历史记录替换位置,像这样
this.props.history({pathName: currentPath, state: resetState})
“currentPath”为目标组件路径 resetState是你想要的新值状态
其他回答
你可以用,
this.props.history。Push ("/template",{…响应}) 或 this.props.history。推送("/template", {response: response})
然后,您可以通过以下代码访问/template组件中的解析数据:
Const state = this.props.location.state
阅读更多关于React会话历史管理的信息
For the earlier versions: history.push('/[pathToSomeWhere]', yourData); And get the data in the related component just like below: this.props.location.state // it is equal to yourData For the newer versions the above way works well but there is a new way: history.push({ pathname: '/[pathToSomeWhere]', state: yourData, }); And get the data in the related component just like below: Class Component this.props.location.state; // it is equal to yourData Function Component const location = useLocation(); location.state; // it is equal to yourData
有时需要使用Link或NavLink组件,而不是使用历史记录。推动作用。你可以用下面的like:
<Link
to={{
pathname: '/[pathToSomeWhere]',
state: yourData
}}
>
...
</Link>
提示:状态键名称应在最新版本中使用。
要使用React 16.8(带hooks)功能组件,您可以使用这种方式 我们发送PhoneNumber到Next Page Login.js
import { useHistory } from 'react-router-dom';
const history = useHistory();
const handleOtpVerify=(phoneNumber)=>
{
history.push("/OtpVerifiy",{mobNo:phoneNumber})
}
<button onClick={handleOtpVerify}> Submit </button>
OtpVerify.js
import useLocation from 'react-router-dom';
const [phoneNumber, setphoneNumber] = useState("")
useEffect(() => {
setphoneNumber(location.state.mobNo)
}, [location]);
return (
<p>We have sent Verification Code to your</p>
<h1>{phoneNumber}</h1>
)
React路由器dom版本6.2.1 useNavigate()已弃用
import { useNavigate } from "react-router-dom";
const navigate = useNavigate()
onClick={() => { navigate('/OtpVerifiy',{mobNo:phoneNumber}) }}
我创建了一个自定义useQuery钩子
import { useLocation } from "react-router-dom";
const useQuery = (): URLSearchParams => {
return new URLSearchParams(useLocation().search)
}
export default useQuery
使用它作为
const query = useQuery();
const id = query.get("id") as string
就这样发送
history.push({
pathname: "/template",
search: `id=${values.id}`,
});
带有钩子的React TypeScript
来自班级
this.history.push({
pathname: "/unauthorized",
state: { message: "Hello" },
});
未经授权的功能组件
interface IState {
message?: string;
}
export default function UnAuthorized() {
const location = useLocation();
const message = (location.state as IState).message;
return (
<div className="jumbotron">
<h6>{message}</h6>
</div>
);
}