[英]Disable a field based on another field's value
I am trying to implement a basic feature to disable a field based on another field's value. 我正在尝试实现一个基本功能,以根据另一个字段的值禁用字段。 Here is the code sample:
这是代码示例:
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>
)
};
};
When I run the given code i run into the following error TypeError: undefined is not an object (evaluating 'this.state.options')
. 当我运行给定的代码时,我遇到以下错误
TypeError: undefined is not an object (evaluating 'this.state.options')
。 It is a really basic example as I am still a newbie and I am trying to understand how this code sample really works. 这是一个非常基本的例子,因为我仍然是一个新手,我试图理解这个代码示例是如何工作的。 Any help is appreciated.
任何帮助表示赞赏。 Thanks.
谢谢。
es6 classes don't autobind, if you use a property initializer it will scope this
to your class: ES6类不autobind,如果你使用属性初始化它将范围
this
给你的类:
onChange = (value) => {
//
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.