Javascript 将新值呈现为TextInput React Native

Javascript 将新值呈现为TextInput React Native,javascript,android,ios,reactjs,react-native,Javascript,Android,Ios,Reactjs,React Native,我有一个react原生表单,带有一个按钮,可以根据一些用户的信息自动填充一些字段。关键是,即使我更新了与TextInput相关的状态变量,TextInput也不会显示这样的数据。为了简单起见,这里有一个简短的片段 导出默认类组件扩展React.Component{ 建造师(道具){ 超级(道具); 此.state={ 值:null } } 自动编译=()=>{ this.setState({“value”:“new value”}) } render(){ 返回( { 这是自动编译 }}> 自动

我有一个react原生表单,带有一个按钮,可以根据一些用户的信息自动填充一些字段。关键是,即使我更新了与TextInput相关的状态变量,TextInput也不会显示这样的数据。为了简单起见,这里有一个简短的片段

导出默认类组件扩展React.Component{
建造师(道具){
超级(道具);
此.state={
值:null
}
}
自动编译=()=>{
this.setState({“value”:“new value”})
}
render(){
返回(
{
这是自动编译
}}>
自动编译
this.setState({'value':value})
value={this.state.value}
/>
)
}
}
}

在本例中,如果单击“自动编译”,下面的TextInput将不会显示新值,即使状态变量将被更新。我的问题是,如何在不输入的情况下从外部更新TextInput显示值?

您需要更改此设置

  autocompile = () => {
        this.setState({"value": "new value"})
  }
    

还有这个

 onChangeText={(value) => this.setState({'value': value})}


类组件解决方案

import React from 'react';
import { Text, View, TextInput, TouchableOpacity } from 'react-native';

export default class Component extends React.Component {
  constructor(props) {
    super(props);
    this.state = { value: '' };
    this.autocompile = this.autocompile.bind(this);
  }

  autocompile() {
    this.setState({ value: 'new value' });
  }

  render() {
    return (
      <View>
        <TouchableOpacity onPress={this.autocompile}>
          <Text>Autocompile</Text>
        </TouchableOpacity>
        <TextInput
          onChangeText={(value) => this.setState({ value: value })}
          value={this.state.value}
        />
      </View>
    );
  }
}
import React, { useState } from 'react';
import { View, TouchableOpacity, Text, TextInput } from 'react-native';

const App = () => {
  const [value, setValue] = useState('');

  const autocompile = () => setValue('new value');

  return (
    <View>
      <TouchableOpacity onPress={() => autocompile()}>
        <Text>Autocompile</Text>
      </TouchableOpacity>
      <TextInput onChangeText={(value) => setValue(value)} value={value} />
    </View>
  );
};

export default App;

同样,每当我单击文本输入时,都不会显示新值
import React from 'react';
import { Text, View, TextInput, TouchableOpacity } from 'react-native';

export default class Component extends React.Component {
  constructor(props) {
    super(props);
    this.state = { value: '' };
    this.autocompile = this.autocompile.bind(this);
  }

  autocompile() {
    this.setState({ value: 'new value' });
  }

  render() {
    return (
      <View>
        <TouchableOpacity onPress={this.autocompile}>
          <Text>Autocompile</Text>
        </TouchableOpacity>
        <TextInput
          onChangeText={(value) => this.setState({ value: value })}
          value={this.state.value}
        />
      </View>
    );
  }
}
import React, { useState } from 'react';
import { View, TouchableOpacity, Text, TextInput } from 'react-native';

const App = () => {
  const [value, setValue] = useState('');

  const autocompile = () => setValue('new value');

  return (
    <View>
      <TouchableOpacity onPress={() => autocompile()}>
        <Text>Autocompile</Text>
      </TouchableOpacity>
      <TextInput onChangeText={(value) => setValue(value)} value={value} />
    </View>
  );
};

export default App;