2017-10-18 11 views
1

mobx reactがcomponentWillReactという新しいライフサイクルを提供していることをドキュメントで読んでいます。しかし、私のクラスはrender関数のmobxの変化に反応するようです。私の店が変わると、componentWillReactは決して引き起こされません。MobX componentWillReact not firing

私は小道具として「次へ」を送ります。このアプリはmobxを使わない。

import { observer } from 'mobx-react'; 


@observer class QuickShopNew extends Component { 
    componentWillReact() { 
     console.log(this.props.store.next); 
    } 

    render(){ 
     //console.log(this.props.store.next); 
     return(
      <div> 
      </div> 
     ) 
    } 
} 
+0

simple working exampleがどのように読むことができます

あなたはあなたの質問であなたの 'store'が含まれてもらえますか?それは何が間違っているのかという手がかりを与えるかもしれません。 – Tholle

答えて

1

あなたのコンポーネントは、レンダリングメソッドで観察されたプロパティを参照解除していないことがわかります。そのためmobxはコンポーネントを再レンダリングする必要があり、valueWithReactを値変更に呼び出す必要があることを知らないのです。あなたがここにオブザーバーコンポーネントの作業here

とはcodepen

const { Component, PropTypes } = React; 
const { observable } = mobx; 
const { observer } = mobxReact; 


// Store for state 
class Store { 
    @observable next = 0; 
    increaseNext =() => this.next +=1; 
} 

let store = new Store(); 
@observer 
class MyComponent extends Component { 
    componentWillReact() { 
     console.log(this.props.store.next); 
    } 
    render() { 
    return (
     <div> 
     <h1>{this.props.store.next}</h1> 
     </div> 
    ); 
    } 
} 

class App extends Component { 
    render() { 
    return (
     <div> 
      <MyComponent 
      store={store} 
      /> 
     <button onClick={store.increaseNext}> 
      Increase 
     </button> 
     </div> 
    ); 
    } 
} 

// Insert into container 
ReactDOM.render(<App />, document.getElementById('app'));