我正在尝试实现一项基本功能,以基于另一个字段的值禁用一个字段。这是代码示例:
import React from 'react'
import t from "tcomb-form-native";
import {Text, View, TextInput, TouchableHighlight } from 'react-native';
const Form = t.form.Form;
var Type = t.struct({
disable: t.Boolean, // if true, name field will be disabled
name: t.String
});
var options = {
fields: {
name: {}
}
};
export default class App extends React.Component {
constructor(props){
super(props);
this.state = {
options: options,
value: null
}
}
onChange(value) {
var newOptions = t.update(this.state.options, {
fields: {
name: {
editable: {'$set': !value.disable}
}
}
});
this.setState({options: newOptions, value: value});
}
onPress() {
var value = this.refs.form.getValue();
if (value) {
console.log(value);
}
}
render() {
return (
<View style>
<Form
ref="form"
type={Type}
options={this.state.options}
value={this.state.value}
onChange={this.onChange}
/>
<TouchableHighlight style onPress={this.onPress} underlayColor='#99d9f4'>
<Text style>Save</Text>
</TouchableHighlight>
</View>
)
};
};
当我运行给定的代码时,我遇到以下错误TypeError: undefined is not an object (evaluating 'this.state.options')
。这是一个非常基本的示例,因为我仍然是新手,我试图了解此代码示例的实际工作方式。任何帮助表示赞赏。谢谢。
es6类不会自动绑定,如果使用属性初始化器,它将作用this
于您的类:
onChange = (value) => {
//
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句