如何将React native中的TextInput值传递给mobx动作?

时间:2018-05-21 15:15:21

标签: reactjs native mobx

我想将文本输入的值传递给TestStore(mobx)以基本警告输入的值,请任何人都可以告诉我如何实现这一点。

由于

我有以下代码:

Home.js:

<TextInput style={[
  !this.state.email ? { borderWidth: 3, borderColor: 'red' } : null]}
  onChangeText={(text) => this.validate(text, 'email')}
  value={this.state.email}
  maxLength={100} 
/>

<TouchableOpacity style={
  [{
    width: '70%',
    backgroundColor: this.state.buttonInvalid ? '#607D8B' : '#009688' }
  ]} 
  activeOpacity={.5}
  disabled={this.state.buttonInvalid}
  onPress={this.props.testStore.alert}
>
  <Text>Submit</Text>
</TouchableOpacity>

TestStore.js:

 import { observable, action } from "mobx";

 export default class TestStore {

   @observable email = null;
     @action alert = () => {  
        alert('The value you entered is '); 
    }
}

1 个答案:

答案 0 :(得分:0)

好的,解决了它将按钮修改为以下代码:

<TouchableOpacity style={
  [{
    width: '70%',
    backgroundColor: this.state.buttonInvalid ? '#607D8B' : '#009688' }
  ]} 
  activeOpacity={.5}
  disabled={this.state.buttonInvalid}
 onPress={() => this.props.testStore.alert(this.state.email)}
>
  <Text>Submit</Text>
</TouchableOpacity>