我是一种新的reactjs,我正在一步一步学习。我遇到了一个问题,那就是当我尝试访问道具中的位置参数时,它返回我undefined
。我试图找到解决办法,但大多数人都写我要补充我的组件中的路由器,但我包裹它在路由器,但仍,我没有获得位置参数位置缺少道具(ReactJS)
export const abcdContainer = withRouter(connect(
mapStateToProps,
mapDispatchToProps
)(abcd));
我试图访问和组件中的位置参数,但问题是它中没有位置参数。谁能告诉我它是什么,是怎么了?
请,如果有人知道什么是错了,请告诉我,我已经花了我半天,我无法弄清楚
代码和版本添加了URL
路由器版本=> 2.8.1
网址:http://localhost:3000/somePage?someParam=cm9oYW5qYWxpbHRlYWNoZXJAZ21haWwuY29t
abcdContainer.js
const mapStateToProps = (state, ownProps) => {
// some code over here
}
const mapDispatchToProps = (dispatch, ownProps) => {
// some code over here
};
export const abcdContainer = withRouter(connect(
mapStateToProps,
mapDispatchToProps
)(abcd));
abcd.jsx
class abcd extends Component {
constructor(props, context) {
super(props, context);
this.state = {
// setting state over here
};
}
abcdFunction(){
if (this.props.location.query.someParam !== undefined){ // trying to extract someParam value from location
// some code over here
}
}
render() {
// some code over here
}
}
export default CSSModules(abcd, styles, { allowMultiple: true });
这里是流。路由器重定向到容器中,然后将容器重定向到真正的组件
Route.js
export const Routes = ({ store }) => (
<Provider store={store}>
<Router history={browserHistory}>
<Route path="/" component={aContainer}>
<IndexRoute component={IContainer} />
// some routes
<Route path="/openAbcd" component={() => (<abcdContainer caller="aaaaaaa" />)} />
// some routes
</Route>
// some routes
</Router>
</Provider>
);
Routes.propTypes = {
store: React.PropTypes.object.isRequired,
};
你必须提供一个更完整的例子,否则将很难说。发布包含组件的代码的最小但完整的示例。你正在使用哪个版本的react-router? – trixn
@trixn我已经添加了代码和版本 – Gardezi
您是否期望位置对象成为窗口中的一个或命名与此类似?在任何情况下,它都不是道具,因为没有父组件传递给孩子。 – terpinmd