2017-04-04 14 views
0

を反応させることコンバートconstが、私はクラスにこのコードを変更することができますどのようにクラス

<Switch> 
       <Route exact path="/" component={About} /> 
       <Route exact path="/login" component={Login} /> 
       <PrivateRoute path="/protected" component={Home}/> 
       <Route component={NotMatch} /> 
</Switch> 

しかし、私はに変更する必要があります私がMobXを使っているので、クラス。このような

何か:

@inject('store') 
@observer 
export default class PrivateRoute extends Component { 
    constructor (props) { 
    super(props) 
    this.store = this.props.store 

    } 

    render() { 
    return (
      <div> 

      </div> 
    ) 
    } 
} 

ありがとう!

答えて

2

あなたのクラスコードがあなたの機能性成分などの小道具を処理するためのOKです:

@inject('store') 
@observer 
export default class PrivateRoute extends Component { 
    constructor (props) { 
    super(props); 
    this.store = this.props.store; 
    } 

    render() { 
    const { component, ...rest } = this.props; 
    return (
     <Route {...rest} render={ props => (
      fakeAuth.isAuthenticated ? (
       React.createElement(component, props) 
      ) : (
       <Redirect to={{ 
        pathname: '/login', 
        state: { from: props.location } 
       }}/> 
      ) 
     )}/> 
    ) 
    } 
} 
+0

すべてが正常に動作していますが、へのconstを追加する必要があります:{コンポーネント、...残り} = this.props。 – chemitaxis

+0

何が残りますか? :)ありがとう! – chemitaxis

+1

ああ、constを忘れて、ありがとう! '... rest'は' this.props'オブジェクトの残りの部分を残りのオブジェクトに置きます。 [このリンク](https://babeljs.io/docs/plugins/transform-object-rest-spread/)をご覧ください。 – HosseinAgha

関連する問題