簡體   English   中英

根據其他字段的值禁用字段

[英]Disable a field based on another field's value

我正在嘗試實現一個基本功能,以根據另一個字段的值禁用字段。 這是代碼示例:

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類不autobind,如果你使用屬性初始化它將范圍this給你的類:

onChange = (value) => {
  //
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM