2016-10-01 17 views
1

私は別のコンポーネントをレンダリングするコンポーネントを持っています。私がサブコ​​ンポーネントを変更すると、メインコンポーネントが再レンダリングされます。第二成分からonSubmit以下の例で反応ネイティブのサブコンポーネントの変更に親コンポーネントを再レンダリングする方法

は、主成分で_onSubmitをトリガするが、setStateではなく再レンダリングビュー

アイデアしていますか?

class MainLayout extends Component { 
    constructor(props) { 
    super(props); 

    this.state = { 
     data: 'no', 
    }; 

    this._onSubmit = this._onSubmit.bind(this); 
    } 

    // this get's triggered by _checkSubmitReady() on the second component 
    _onSubmit(data) { 
    // this state get's set, but this component is not re-rendered 
    // i assume render() should be called here 
    this.setState({data: data}); 
    } 

    render() { 
    return (
     <View><SecondLayout onSubmit={this._onSubmit}/>{this.state.data}</View> 
    ); 
    } 
} 


class SecondLayout extends Component { 
    constructor(props) { 
    super(props); 

    this._checkSubmit = this._checkSubmit.bind(this); 
    } 

    _checkSubmit() { 
    this.props.onSubmit('yes'); 
    } 

    // sub component is mounted, call onSubmit() on parent component 
    componentDidMount() { 
    this._checkSubmit(); 
    } 

    render() { 
    return (
     <View><Text>Nothing here</Text></View> 
    ); 
    } 
} 

答えて

1

試してみてください。

_onSubmit(data) { 
    this.setState({ data: data },() => { 
    this.forceUpdate(); 
    }); 
} 

それとも、ES5を使用している場合:

_onSubmit(data) { 
    this.setState({ data: data }, function() { 
    this.forceUpdate(); 
    }.bind(this)); 
} 
関連する問題