首页 > 解决方案 > 如何在 React-Native 中使用 event.value

问题描述

该事件在 React-Native 中不起作用。
这是文本输入。

      <TextInput
          id="name"
          placeholder="name"
          onChange = {this._inputChange}

            />

这就是 onChage 函数。
当我检查在 TextInput 中写入的 console.log(e.nativeEvent.id) 时,它显示未定义。
我猜 e.target.id 不能在 React-native 而不是 React 中使用。
你能推荐一些想法吗?

_inputChange = (e) => {
    let nextState = {};
    console.log(e.nativeEvent.id)
    nextState[e.nativeEvent.id] = e.nativeEvent.value;
    this.setState(nextState);
}

标签: react-native

解决方案


我就是这样处理的...

handleChange = (field, value) ={
  this.setState({ [field]: value });
}

<TextInput
  onChangeText={this.handleChange.bind(this, 'name')}
  ...
  />

推荐阅读