2017-06-01 45 views

回答

0

action也是transaction,這意味着在動作完成後,將重新計算您在動作中更改的可觀察值的任何值。如果您不包裝action中的函數,則可能會多次計算派生值。

實施例 - 重新計算後的動作(JS Bin

@observer 
class App extends Component { 
    @observable x = 'a'; 
    @observable y = 'b'; 
    @computed get z() { 
    console.log('computing z...'); 
    return `${this.x} ${this.y}`; 
    } 

    onClick = action(() => { 
    this.x = 'c'; 
    this.y = 'd'; 
    }); 

    render() { 
    return <div onClick={this.onClick}> {this.z} </div>; 
    } 
} 

實施例 - 重新計算直線距離(JS Bin

@observer 
class App extends Component { 
    @observable x = 'a'; 
    @observable y = 'b'; 
    @computed get z() { 
    console.log('computing z...'); 
    return `${this.x} ${this.y}`; 
    } 

    onClick =() => { 
    this.x = 'c'; 
    this.y = 'd'; 
    }; 

    render() { 
    return <div onClick={this.onClick}> {this.z} </div>; 
    } 
} 
相關問題