2017-06-20 99 views
0

說,在作出反應我有一個分量呈現爲這樣:純組分和風格/參考標籤

render() { 
    return (<AComponent ref={(ref) => { this.ref = ref}} style={{height: 110}} />); 
} 

我查道具兌nextProps,

componentWillReceieveProps(nextProps) { 
    if (this.props !== nextProps) { 
     this.setState({ ..this.getState(nextProps) }); 
    } 
} 

他們會永遠不淺相等,因爲樣式道具正在被賦予一個對象字面值並且引用一個匿名函數?或者React有什麼地方不考慮道具的風格和參考標籤?如果是這樣,是否還有其他排除?

回答

2

refkey是特殊的,並沒有真正作爲道具傳遞。

style只是一個普通的道具。 this.props !== nextProps永遠是真的;至少你需要淺淺地比較這些,但你是對的,style={{height: 110}}this.props.style === nextProps.style將永遠是錯誤的。您可以通過將樣式拉入外部變量並使用該外部變量來取代內聯對象。