2016-11-21 79 views
3

當我嘗試設置一個變量時,組件僅在fetch完成後才更新。這是一個exapmle代碼。React relay pendingVariables在setVariables後總是爲空

class RelayExample extends React.Component { 
    setVars() { 
    this.props.relay.setVariables({id: '2'}); 
    }; 
    render() { 
    console.log(this.props.relay.pendingVariables); 
    return (
     <div> 
     <button onClick={this.setVars.bind(this)}>set variable</button> 
     </div> 
    ); 
    } 
} 

RelayExample = Relay.createContainer(RelayExample, { 
    initialVariables: { 
    id: '1' 
    }, 
    fragments: { 
    userStore:()=> { 
     return Relay.QL` 
     fragment on userStore { 
     user(id:$id){ 
      email 
     } 
     } 
     `; 
    } 
    } 
}); 

ReactDOM.render(
    <Relay.RootContainer Component={RelayExample} route={new TestQuery()}/>, 
    document.getElementById('app') 
); 

當我按下按鈕,我得到在控制檯這樣的結果: enter image description here

即使我使用forceUpdate的setVariables功能之後,我得到類似的結果。

setVars() { 
     this.props.relay.setVariables({id: '2'}); 
     this.forceUpdate(); 
}; 

enter image description here

+0

環境是Relay.Store –

回答

-1

我相信這是預期的行爲。

pendingVariables包含正被用來 獲取新道具組變量,即,當this.props.relay.setVariables()或 this.props.relay.forceFetch()被調用,而對應的要求 正在飛行中。

如果沒有請求在飛行中pendingVariables爲空。

如果您在設置變量後立即登錄控制檯,您應該在pendingVariables中看到它們。

+0

如果我沒有錯,根據[this](https://facebook.github.io/relay/docs/api-reference-relay-container.html#pendingvariables)例子組件必須在setVariables之後更新並且必須滿足pendingVariables。 –

+1

這不是所說的行爲。這裏有同樣的問題。 – whitep4nther