【发布时间】:2021-04-30 02:07:34
【问题描述】:
我正在尝试在路由组件中传递道具,我知道我们不能直接传递给它,所以我曾经渲染,但是道具在子组件中仍然未定义。
import React from 'react';
//components
import Register from '../components/register/register';
import Login from "../components/login/login";
import ForgetPassword from '../components/forget-password/forget-password';
//redux
import {store} from "../redux/store";
import { connect } from 'react-redux';
import actions from "../redux/authentication/actions";
//react-router
import {BrowserRouter,Route,Switch} from "react-router-dom";
//antd
import "antd/dist/antd.css";
//css
import '../global/_global.scss';
function Authentication(props) {
console.log("PROPS", props)
return (
<div className="App">
<BrowserRouter>
{/*switch-component will render first that matches the includes path*/}
<Switch>
<Route path='/login' component={Login} />
<Route exact path='/' component={Login} />
<Route path='/register'
render={(props) => (
<Register {...props} check={props.registerUser} />
)}
/>
<Route path='/forget-password' component={ForgetPassword} />
</Switch>
</BrowserRouter>
</div>
);
}
function mapStateToProps(state){
return state.reducers;
}
export default connect(mapStateToProps, actions)(Authentication);
【问题讨论】:
-
尝试将
render={(props) => (更改为render={() => ( -
它工作正常。谢谢,但是如果我这样做了,为什么它不起作用。
-
因为您尝试传递的道具不是来自
render函数,而是来自您在Authentication组件中收到的道具。
标签: javascript reactjs react-redux react-router