2016-09-23 82 views
1

我想檢索功能組件內的用戶輸入(我正在使用redux體系結構)。但是,當我參考項目console.log(),我得到一個構造函數,而不是實際的對象。TextInput通過引用獲取值本機

如何在不操縱狀態的情況下獲得用戶輸入?

<Modal visible={visibleModal === 'addRoom'} onRequestClose={() => null}> 
    <TextInput ref={el => {roomName = el}} style={styles.input} /> 
    <Button onPress={() => store.dispatch(hideModal())}>Cancel</Button> 
    <Button onPress={() => { 
     store.dispatch(addRoom({name: roomName.value})) 
     return store.dispatch(hideModal()) 
    }}>OK</Button> 
    </Modal> 
+0

根據文檔'TextInput ref = {(c)=> this._input = c}',您都需要返回該值,並可能使用此值。在構造函數中看到它。 – ajmajmajma

回答

1

訪問您的TextInput您無法通過裁判得到一個TextInput的值與本地做出反應。從輸入中獲取文本的唯一方法是訂閱更改事件。

-1

嘗試.value當您通過ref

+0

這可能在React網頁上使用元素,但它不適用於React Native –