2016-06-29 3 views
0

私はReact JSで多段階登録フォームを持っています。私の主なコンポーネントは以下の通りです:リアボタンで戻るボタンが多段階で動作しない

import React, {PropTypes} from 'react'; 
import {connect} from 'react-redux'; 
import {bindActionCreators} from 'redux'; 
import * as actionCreators from '../../actions/actionCreators'; 
import countries from '../../data/countries'; 

import RegistrationFormStepOne from './registrationFormStepOne'; 
import RegistrationFormStepTwo from './registrationFormStepTwo'; 
import RegistrationFormStepThree from './registrationFormStepThree'; 
import RegistrationFormStepFour from './registrationFormStepFour'; 

class RegistrationPage extends React.Component { 
    constructor(props) { 
     super(props); 

     this.state = { 
      user: Object.assign({}, this.props.userData), 
      fileNames: {}, 
      selectedFile: {}, 
      icons: { 
       idCard: 'upload', 
       statuten: 'upload', 
       blankLetterhead: 'upload', 
       companyPhoto: 'upload' 
      }, 
      step: 1, 
      errors: {} 
     }; 

     this.setUser = this.setUser.bind(this); 
     this.onButtonClick = this.onButtonClick.bind(this); 
     this.onButtonPreviousClick = this.onButtonPreviousClick.bind(this); 
    } 

    getCountries(){ 
     return countries; 
    } 

    setUser(event) { 
     const field = event.target.name; 
     const value = event.target.value; 

     let user = this.state.user; 
     user[field] = value; 
     this.setState({user: user}); 
    } 

    onButtonClick(name, event) { 
     event.preventDefault(); 
     this.props.actions.userRegistration(this.state.user); 
     switch (name) { 
      case "stepFourConfirmation": 
       this.setState({step: 1}); 
       break; 
      case "stepTwoNext": 
       this.setState({step: 3}); 
       break; 
      case "stepThreeFinish": 
       this.setState({step: 4}); 
       break; 
      default: 
       this.setState({step: 2}); 
     } 
    } 

    onButtonPreviousClick(){ 
     this.setState({step: this.state.step - 1}); 
    } 

    render() { 
     const languageReg = this.props.currentLanguage.default.registrationPage; 

     let formStep = ''; 
     let step = this.state.step; 
     switch (step) { 
      case 1: 
       formStep = (<RegistrationFormStepOne user={this.props.userData} 
                onChange={this.setUser} 
                onButtonClick={this.onButtonClick} 
                countries={this.getCountries(countries)} 
                errors={this.state.errors} 
                step={step}/>); 
       break; 
      case 2: 
       formStep = (<RegistrationFormStepTwo user={this.props.userData} 
                onChange={this.setUser} 
                onButtonClick={this.onButtonClick} 
                onButtonPreviousClick={this.onButtonPreviousClick} 
                errors={this.state.errors}/>); 
       break; 
      case 3: 
       formStep = (<RegistrationFormStepThree user={this.props.userData} 
                 onFileChange={this.onFileChange} 
                 onButtonClick={this.onButtonClick} 
                 onButtonPreviousClick={this.onButtonPreviousClick} 
                 errors={this.state.errors} 
                 fileNames={this.state.fileNames} 
                 icons={this.state.icons} 
                 fileChosen={this.state.selectedFile}/>); 
       break; 

      default: 
       formStep = (<RegistrationFormStepFour user={this.props.userData} 
                 onChange={this.setUser} 
                 onChangeCheckboxState={this.changeCheckboxState} 
                 onButtonClick={this.onButtonClick} 
                 onButtonPreviousClick={this.onButtonPreviousClick} 
                 errors={this.state.errors}/>); 
     } 

     return (
      <div className="sidebar-menu-container" id="sidebar-menu-container"> 

       <div className="sidebar-menu-push"> 

        <div className="sidebar-menu-overlay"></div> 

        <div className="sidebar-menu-inner"> 
         <div className="contact-form"> 
          <div className="container"> 
           <div className="row"> 
            <div className="col-md-10 col-md-offset-1 col-md-offset-right-1"> 
             {React.cloneElement(formStep, {currentLanguage: languageReg})} 
            </div> 
           </div> 
          </div> 
         </div> 
        </div> 
       </div> 
      </div> 
     ); 
    } 
} 

RegistrationPage.contextTypes = { 
    router: PropTypes.object 
}; 

function mapStateToProps(state, ownProps) { 
    return { 
     userData: state.userRegistrationReducer 
    }; 
} 

function mapDispatchToProps(dispatch) { 
    return { 
     actions: bindActionCreators(actionCreators, dispatch) 
    }; 
} 

export default connect(mapStateToProps, mapDispatchToProps)(RegistrationPage); 

[次へ]をクリックすると、ステートステップが変わり、次のフォームステップがレンダリングされます。前のボタンをクリックすると、そのステップは減分され、前のステップが表示されます。それらのボタンでうまく動作します。

しかし、ブラウザの戻るボタンが機能していません。例えば私が3番目のステップにいるとき、ブラウザの戻るボタンをクリックすると、私はホームページにリダイレクトされます。次のように

私routes.jsファイルは次のとおりです。

import React from 'react'; 
import {IndexRoute, Route} from 'react-router'; 

import App from './components/App'; 
import HomePage from './components/HomePage'; 
import Registration from './components/registration/RegistrationPage'; 
import RegistrationStepOne from './components/registration/registrationFormStepOne'; 
import RegistrationStepTwo from './components/registration/registrationFormStepTwo'; 
import RegistrationStepThree from './components/registration/registrationFormStepThree'; 
import RegistrationStepFour from './components/registration/registrationFormStepFour'; 
import UserPage from './components/user/userHome'; 
import requireAuth from './common/highOrderComponents/requireAuth'; 
import hideIfLoggedIn from './common/highOrderComponents/hideIfLoggedIn'; 

import PasswordReset from './common/passwordReset'; 

const routes = (
    <Route path="/" component={App}> 
     <IndexRoute component={HomePage}/> 
     <Route path="registration" component={hideIfLoggedIn(Registration)} /> 
     <Route path="reset-password" component={PasswordReset} /> 
     <Route path="portal" component={requireAuth(UserPage)} /> 
    </Route> 
); 

export default routes; 

私はおそらく<Route path="registration" ...内のこれらのコンポーネントを追加する必要があります、と私は成功せず、それを試してみました。

ブラウザのボタンを元の状態に戻して前の手順に戻す方法を教えてください。

EDIT

次のようにムーroutes.jsファイルは以下のようになります。

const routes = (
<Route path="/" component={App}> 
    <IndexRoute component={HomePage}/> 
    <Route path="registration/:id" component={hideIfLoggedIn(Registration)}/> 
    <Route path="reset-password" component={PasswordReset} /> 
    <Route path="portal" component={requireAuth(UserPage)} /> 
</Route> 
); 

そして、私はに機能(ここで、ステップ刻み)をonButtonClick変更した私の主成分で:

onButtonClick(name, event) { 
    event.preventDefault(); 

    switch (name) { 
     case "stepFourConfirmation": 
      if(this.validation("four")) { 
       this.props.actions.userRegistrationThunk(this.state.user); 
       this.setState({step: 5, user: {}}); 
      } 
      break; 
     case "stepTwoNext": 
      if(this.validation("two")) { 
       this.setState({step: 3}); 
       this.context.router.push("stepThree"); 
      } 
      break; 
     case "stepThreeFinish": 
      this.setState({step: 4}); 
      break; 
     default: 
      if(this.validation("one")) { 
       this.setState({step: 2}); 
       this.context.router.push('stepTwo'); 
      } 
    } 
} 

さらに追加しました

componentWillMount(){ 
    console.log(this.props.params.id); 
} 

ここで、ブラウザの戻るボタンをクリックすると、ステップを減らします。

最初にメインコンポーネントをロードするとき、私はコンソールのstepOneに入り、それは良いことです。しかし、次のボタンをクリックすると、2番目のステップに進みます。 URLはregistration/stepTwoに変更されますが、コンソールにはという警告が表示されます。警告:[react-router]ロケーション「stepTwo」はいずれのルートとも一致しません。とは何ですか、私にはthis.props.params.idが表示されません。

アドバイスはありますか?

答えて

0

あなたのコンポーネント内のステップを管理しているのではなく。あなたが登録ページ用

const routes = (
    <Route path="/" component={App}> 
     <IndexRoute component={HomePage}/> 
     <Route path="registration/:id" component={hideIfLoggedIn(Registration)} > 
</Route 
     <Route path="reset-password" component={PasswordReset} /> 
     <Route path="portal" component={requireAuth(UserPage)} /> 
    </Route> 
); 

をルートを変更していたルートはまたincrementStepルート/登録/ 1とidを変更clickinする内部利用できるようになるためですコンポーネントはthis.props.params.idとして

+0

私の質問が更新されました。 – Boky

関連する問題