reactjs - 如何在 browserHistory.push(path, [state]) 中传递状态
问题描述
我正在开发一个使用 react-router 3.0.5 的项目,我正在尝试传递状态browserHistory.push
并且路由发生变化,但状态不会传递到任何位置的道具。
我已经阅读了文档,但我可能会遗漏一些简单的东西。据我所知,这应该是发送状态没有问题。
async handleSubmit(e) {
e.preventDefault();
const email = this.email.value;
const password = this.password.value;
const passwordconfirm = this.passwordconfirm.value;
const profile = {
firstName: this.firstName.value,
lastName: this.lastName.value,
firstTime: !!this.firstTime.value,
dueDate: date,
couponCode,
membership: { level: 'gold' },
};
Accounts.createUser({ email, password, profile }, (err) => {
if (err) {
return self.setError(err.message);
}
if (isGift) {
claimGift.call({ code: couponCode }, () => {
swal({
title: 'Gift Claimed!',
icon: 'success',
button: 'Continue',
}).then((confirm) => {
if (confirm) return browserHistory.push('/payment', { level: 'gold' });
});
});
} else return browserHistory.push('/payment', { level: 'gold' });
});
}
}
包.json
{
"name": "fakename",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "meteor --settings dev-settings.json"
},
"dependencies": {
"autoprefixer": "^7.1.1",
"babel-runtime": "^6.26.0",
"bcrypt": "^1.0.3",
"bluebird": "^3.5.1",
"contentful": "^5.1.3",
"marked": "^0.3.6",
"meteor-node-stubs": "~0.2.4",
"moment": "^2.21.0",
"prop-types": "^15.6.0",
"radium": "^0.19.6",
"react": "^16.1.1",
"react-async-script-loader": "^0.3.0",
"react-burger-menu": "^2.1.11",
"react-document-title": "^2.0.3",
"react-dom": "^16.1.1",
"react-feather": "^1.0.7",
"react-flatpickr": "^3.6.3",
"react-router": "3.0.5",
"react-stripe-elements": "^1.2.1",
"react-tooltip": "^3.4.0",
"reactable": "github:vladnicula/reactable",
"sib-api-v3-sdk": "^3.1.6",
"simpl-schema": "^0.3.2",
"stripe": "^5.5.0",
"sweetalert": "^2.0.8",
"sweetalert2": "^7.1.2",
"underscore": "^1.8.3"
},
"devDependencies": {
"@meteorjs/eslint-config-meteor": "^1.0.5",
"babel-eslint": "^7.2.3",
"eslint": "^4.4.1",
"eslint-config-airbnb": "^15.1.0",
"eslint-import-resolver-meteor": "^0.4.0",
"eslint-plugin-import": "^2.7.0",
"eslint-plugin-jsx-a11y": "^5.1.1",
"eslint-plugin-meteor": "^4.0.1",
"eslint-plugin-react": "^7.2.1"
},
"postcss": {
"plugins": {
"postcss-easy-import": {
"extensions": [
".css",
".scss",
".import.css"
]
},
"autoprefixer": {
"browsers": [
"last 2 versions"
]
}
}
},
"eslintConfig": {
"parser": "babel-eslint",
"parserOptions": {
"allowImportExportEverywhere": true,
"allowAfterThis": true
},
"plugins": [
"meteor"
],
"extends": [
"airbnb",
"plugin:meteor/recommended"
],
"settings": {
"import/resolver": "meteor"
},
"rules": {
"import/extensions": [
"off",
"never"
],
"import/no-extraneous-dependencies": "off",
"import/prefer-default-export": "off",
"import/no-absolute-path": "off",
"react/prefer-stateless-function": "off",
"react/jsx-filename-extension": "off",
"react/forbid-prop-types": "off",
"react/require-default-props": "off",
"no-underscore-dangle": "off",
"jsx-a11y/no-static-element-interactions": "off",
"class-methods-use-this": "off"
}
}
}
解决方案
React Router 允许我们通过browserHistory
和传递状态Link
。下面给出了示例代码片段。
browserHistory.push({pathname: '/navigationPath', state: {message: 'navigated'}})
在您的情况下,您尚未添加pathname
and state
in browserHistoy
。您的代码应如下所示
browserHistory.push({pathname: '/payment', state: {level: 'gold'}})
希望此解决方案对您有所帮助。
推荐阅读
- python - `mp.pool` 正常工作时`mp.pool.ThreadPool` 失败
- javascript - React-Router V2, V4 - 如何在 react-router V4 中使用 react-router V2 的“路由器”?
- typescript - 量角器 + 茉莉花 + TS。打字问题:xit 返回无效。如何使用 xit().pend(原因)
- vba - 选择现有形状时出现错误 86db089e
- java - 如何在java中获取表情符号代码?
- reactjs - 道具类型在反应应用程序中不起作用
- android - Android - DreamService 中的 setRequestedOrientation
- node.js - 错误:使用 fs.outputJsonSync 时没有这样的文件
- javascript - Location.go 后重新加载 Angular 6 路由参数
- truetype - True Type 字体缩放