与<redirect>反应路由器4一起传递数据

时间:2018-10-18 05:40:52

标签: reactjs react-router

我有一个index.js文件。它包含单个组件类

export default class extends React.Component {
    constructor(props) {

        this.state = {
            member: false
        }
    }

    componentDidMount() {

        // this.state.member = member
        this.fillMemberWithdata();
    }

    getPath() {
        const {isEmailInOm, isQffConnectOMAccount} = this.state;
        let path;
        if(isEmailInOm) {
            if(isQffConnectOMAccount) {
                // show acc
                path = '/xyz/success-connect-email-qff';
            } else {
                // email in om, but not connect
                path = '/xyz/connect-email-qff';
            }
        } else {
            // email not in OM, search it or create new
            path = '/xyz/is-existed-member';
        }

        return path;
    }

    render() {
        const myPath = this.getPath();

        if(this.state.loading){
            return <p>Loading</p>
        }

        return (
            <Switch>
                <Route exact path="/xyz/connect-email-qff" component={ConnectEmailQff} />
                <Route exact path="/xyz/connect-email-qff1" component={ConnectEmailQff} />
                <Route exact path="/xyz/connect-email-qff2" component={ConnectEmailQff} />
                .....

                <Redirect to={mPath} someData={this.state}/>
            </Switch>
        );
    }
}

这是将数据附加到<Redirect>的一种方法,因此当我在特定组件中时可以访问它。

0 个答案:

没有答案