将TextInput传递给函数时从TextInput中提取值

时间:2017-05-25 11:44:19

标签: reactjs react-native

我有以下内容,TextInput将输入的值传递给函数。我的问题是如何从传递给函数的searchString中提取文本值。在那一刻,它是一个对象。

<TextInput
  style={styles.input}
  placeholder="Search..."
  onChangeText={(searchString) => this.searchChanged({searchString})}
  placeholderTextColor="#555"
/>

searchChanged(searchField) {

        console.log("Search String Changed "+ searchField);

        this.setState({ 
            dataSource: this.state.dataSource.cloneWithRowsAndSections(this.convertNeighbourArrayIntoMap(this.state.dataSourceCopy, "Mike")),
        });

    }

2 个答案:

答案 0 :(得分:0)

这是一个对象,因为你传递了一个对象:

{searchString} - means object

您应该将代码更改为:

  onChangeText={(searchString) => this.searchChanged(searchString)}

答案 1 :(得分:0)

因为您在这里使用{}

this.searchChanged({searchString})

{searchString}的含义是{searchString:searchString},它将转换为:

this.searchChanged({searchString: searchString})

检查此代码段:

let a = 5;

console.log({a});

像这样写:

<TextInput
    style={styles.input}
    placeholder="Search..."
    onChangeText={(searchString) => this.searchChanged(searchString)}
    placeholderTextColor="#555"
/>

searchChanged(searchField) {
    console.log("Search String Changed "+ searchField);
}