2016-10-06 21 views
0

后组件不会更新我每次发生特定事件时都使用direct manipulation来更改视图的颜色。调用`this.forceUpdate();`

我的渲​​染功能:

onSlideChangeHandle(index) { 
    this._view.backgroundColor = 'red' 
    this._view.forceUpdate() 
    } 

我需要做些什么更给力的组件来改变颜色:

render() { 
    return (
     <View> 
     <View 
      ref={component => this._view = component} 
      style={{width: 50, height: 50}} /> 
     </View> 
    ); 

被称为每一个特定的事件发生时我的更新功能?该方法被调用,并且属性被更新。不幸的是,用户界面没有更新,调用强制更新。我想念什么?我知道我应该使用状态来更新组件,但对于这个特定情况,我真的需要直接操作。

+0

你能解释为什么你不能使用状态? – FuzzyTree

回答

0

我发现了这个问题。我需要使用setNativeProps来更新背景颜色。

onSlideChangeHandle(index) { 
    this._view.setNativeProps({ style: { 
     backgroundColor: 'red' 
    } }); 
}