2017-03-28 24 views
1

我已經創建瞭如下的內部回調函數。如何使我的內部回調函數響應mobx狀態更改?

render() { 
    const profiles = this 
     .props 
     .store 
     .profiles; 
    const renderCell = (rowIndex: number) => { 
     console.log('rendering...'); 

     return <Cell>{profiles[rowIndex].alias}</Cell>; 
    }; 
    return (
     <div className="profileList"> 
     <Table 
      allowMultipleSelection={false} 
      numRows={this.props.store.profiles.size} 
      isRowHeaderShown={false} 
      selectionModes={SelectionModes.ROWS_AND_CELLS} 
      isColumnResizable={false} 
      isRowResizable={false} 
      defaultColumnWidth={1024} 
      onSelection={(region) => this.onSelection(region)} 
     > 
      <Column name="Connection Profiles" renderCell={renderCell.bind(this)}/> 
     </Table> 
     </div> 
    ); 
    } 

從上面的代碼中,你可以看到,有是render()函數內部創建了一個renderCell功能。當我更新mbox狀態profile.alias時,此功能不會被觸發。如果我直接將profile.alias置於render()方法下,它可以正常工作。如何讓我的renderCell響應狀態更改?

回答

0

如果Cell組件不是外在的,你可以用@observer

裝飾它如果它的外部,你可以換你的函數中的觀察者,像這樣:

const RenderCell = observer(({ rowIndex }) => { 
    console.log('rendering...'); 

    return <Cell>{profiles[rowIndex].alias}</Cell>; 
}; 

有很大的文檔約Understanding what MobX reacts to在他們的網站上,我建議閱讀它,它可以幫助你更好地理解MobX。

+0

'observer(..)'返回與以前相同的反應組件嗎?完成更改後,我的組件根本不工作。我收到了一個錯誤異常:'警告:有些東西直接調用React組件。改用工廠或JSX。 ' –

+0

您應該將它用作JSX組件調用語法: '} />' 或者您可以使用'React.createFactory(component) '如果您不想使用JSX語法,則使用React文檔編寫的語法:[link](https://facebook.github.io/react/warnings/legacy-factories.html) – AmitBu

相關問題