Reactjs 将react路由器参数传递给调度

Reactjs 将react路由器参数传递给调度,reactjs,parameters,redux,react-router,Reactjs,Parameters,Redux,React Router,我在访问react路由器参数并将其传递给调度功能时遇到问题。我不知道我是否遗漏了什么,但有人能给我指出正确的方向吗。如果没有,我怎么能传递我需要的信息呢。以下是相关代码: export function verifyUser(token) { //here i want to have params form react router object return dispatch => { dispatch(isLoading(true));

我在访问react路由器参数并将其传递给调度功能时遇到问题。我不知道我是否遗漏了什么,但有人能给我指出正确的方向吗。如果没有,我怎么能传递我需要的信息呢。以下是相关代码:

 export function verifyUser(token) { //here i want to have params form react router object
      return dispatch => {
        dispatch(isLoading(true));
        axios.get('/auth/verify/' + token)
        .then(response => {
          dispatch(_verifyUserSuccess(response.data.message));
          dispatch(isLoading(false));
          // browserHistory.push('/signin');
        })
        .catch(error => {
          if (error.response) {
            dispatch(_verifyUserFailure(error.response.data.message));
            dispatch(isLoading(false));
          }
          else {
            // Something happened in setting up the request that triggered an Error
            console.log(error.message);
          }
        });
      };
    }

    import React, {Component} from 'react';
    import VerificationForm from './VerificationForm';
    import {connect} from 'react-redux';
    import { verifyUser } from '../../actions/actionVerify';
    import { Grid, Row, Col } from 'react-bootstrap';

    class VerificationPage extends Component {
      constructor(props) {
          super(props);
          console.log(this.props);
            const { params } = this.props;
      }
        render() {
            return (
              <Grid>
                        <Row>
                  <Col sm={6} smPush={3}>
                                <VerificationForm onSave={this.props.onSave} message={this.props.message} isLoading={this.props.isLoading}/>
                  </Col>
              </Row>
             </Grid>
            );
        }
    }
    function mapStateToProps(state) {
        return {
            message: state.verify.message,
        isLoading: state.form.isLoading
        };
    }
    function mapDispatchToProps(dispatch) {
      return {
        onSave: () => dispatch(verifyUser(this.props.params.token)) //here when i pass params.token i got undefined when action is called
      };
    }
    export default connect(mapStateToProps, mapDispatchToProps)(VerificationPage);

    <Route path="/" component={App}>
    <IndexRoute component={Greetings}/> 
    <Route path="/signup" component={SignupPage}/>
    <Route path="/signin" component={SigninPage}/>
    <Route path="/verify/:token" component={VerificationPage} />
    </Route>
export function verifyUser(token){//这里我想让参数形成react router对象
返回调度=>{
调度(isLoading(true));
获取('/auth/verify/'+令牌)
。然后(响应=>{
分派(_verifyUserSuccess(response.data.message));
调度(isLoading(false));
//browserHistory.push('/sign');
})
.catch(错误=>{
if(error.response){
分派(_verifyUserFailure(error.response.data.message));
调度(isLoading(false));
}
否则{
//设置触发错误的请求时发生了一些问题
console.log(错误消息);
}
});
};
}
从“React”导入React,{Component};
从“/VerificationForm”导入VerificationForm;
从'react redux'导入{connect};
从“../../actions/actionVerify”导入{verifyUser};
从'react bootstrap'导入{Grid,Row,Col};
类验证页扩展组件{
建造师(道具){
超级(道具);
console.log(this.props);
const{params}=this.props;
}
render(){
返回(
);
}
}
函数MapStateTops(状态){
返回{
消息:state.verify.message,
isLoading:state.form.isLoading
};
}
功能图DispatchToprops(调度){
返回{
onSave:()=>dispatch(verifyUser(this.props.params.token))//在这里,当我传递params.token时,当调用action时,我没有定义
};
}
导出默认连接(mapStateToProps、mapDispatchToProps)(验证页面);

您可以执行以下操作

verifyUser = (token) => {
    return (dispatch) => {
        ...
    }
}
connect(mapstatetops,{verifyUser})(验证页面)

在组件中使用onSave方法

class VerificationPage extends React.Component {
    onSave = (data) => {
        this.props.verifyUser(this.props.params.token);
    }

    ...
}

另一种选择是partials

您可以使用lodash轻松实现这一点:

connect(mapStateToProps, (dispatch) => {
    onSave: (token) => dispatch(verifyUser(token)) // note that I added an argument, here
})(VerificationPage)

...

<VerificationForm onSave={ _.partial(this.props.onSave, this.props.params.token) } ... />
connect(MapStateTrops,(调度)=>{
onSave:(token)=>dispatch(verifyUser(token))//注意,我在这里添加了一个参数
})(验证页)
...
或者不使用lodash,只使用包装回调

<VerificationForm onSave={ () => { this.props.onSave(this.props.params.token) } } ... />
{this.props.onSave(this.props.params.token)}…/>

Sebestian感谢了无数次,效果很好