【问题标题】:How to get the value from a Text Input (a component) to my main app?如何从文本输入(组件)获取值到我的主应用程序?
【发布时间】:2020-07-24 19:55:15
【问题描述】:

我的主应用程序包含一个文本、一个文本输入(一个组件)和一个按钮(另一个组件):

import { StatusBar } from 'expo-status-bar';
import React from 'react';
import { StyleSheet, Text, View, Alert } from 'react-native';
import { Tinput } from './components/Tinput.js';
import { Button } from './components/Button.js';

      export default function App() {
      return (
        <View style={styles.container}>
          <Text style={{fontSize:20, padding:20, textAlign:'center'}}>Ingrese un numero del pokémon a buscar!</Text>
          <Tinput/>
          <Button onPress={()=> ConsultarPokemon(/*this is where i want to insert the value from Tinput */)}> 
            Ingresar 
          </Button> 
          <StatusBar style="auto" />
        </View>
      );
    }

这是我的组件 Tinput,它有文本输入:

import React from 'react';
import { TextInput } from 'react-native';

const Tinput = () => {
  const [numero, setNumero] = React.useState('');

  return (
    <TextInput
      style={{ width:'90%', height: 50, borderColor: 'gray', borderWidth: 1, backgroundColor: '#fffff0', textAlign:'center', borderRadius: 20, borderWidth:5, margin:20}}
      onChangeText={(value) => setNumero({numero: value})}
      value={this.state.numero}
      maxLength={20}
    />
  );
}

export { Tinput };

我想在我的 onPress 函数上使用文本输入中的值,我尝试这样做但没有成功:

 <Button onPress={()=> ConsultarPokemon(Tinput.state.numero)}> 
        Ingresar 
 </Button> 

另外,我的 Tinput 组件有一个错误:undefined is not an object (evalating '_this.state.numero') 所以我可能也以错误的方式使用状态

【问题讨论】:

  • 您不能像这样访问其他组件状态。您只能访问自己的组件状态或访问树中更高组件的状态。所以解决方案是移动使用这两个组件的组件中的状态。
  • 所以我必须给应用程序一个数字状态,然后在我的 Tinput 组件上更改应用程序的状态?我将如何访问该状态? app.setNumber({number:value}) ?

标签: javascript android reactjs react-native native


【解决方案1】:

只有当你创建了像组件类这样的类时,你才会使用 this.state.X,这里有一个例子:

    class Counter extends React.Component {
      constructor(props) {
        super(props);
        this.initialCount = props.initialCount || 0;
        this.state = {
          count: this.initialCount
        }
      }
increment() {
    this.setState({ count: this.state.count + 1 })
  }
  reset() {
    this.setState({ count: this.initialCount})
  }
  render() {
    const { count } = this.state;
    return (
      <>
        <button onClick={this.increment.bind(this)}>+1</button>
        <button onClick={this.reset.bind(this)}>Reset</button>
        <p>Count: {count}</p>
      </>
    );
  }
}

我建议不要把事情复杂化,直接在Tinput里面处理onPress

    const Tinput = () => {
      const [numero, setNumero] = React.useState('');
    
      return (
    <View>
        <TextInput
          style={{ width:'90%', height: 50, borderColor: 'gray', borderWidth: 1, backgroundColor: '#fffff0', textAlign:'center', borderRadius: 20, borderWidth:5, margin:20}}
          onChangeText={(value) => setNumero(value)}
          value={numero} // no need to use this.state.numero
          maxLength={20}
        />
 <Button onPress={()=> ConsultarPokemon(numero)}> 
        Ingresar 
  </Button> 
 </View>
      );
    }

【讨论】:

  • 谢谢,是的,我不应该把它做成一个组件
  • 嘿,很抱歉再次打扰你,我把文本输入放在我的主应用程序上,所以它不再是一个组件了。但它没有采用我插入的值。当我在开始时启动 numero 状态时它工作正常,但是 (value) => setNumero ({ numero: value }) 不使用 then input i insert 当我键入时。而且...是否有必要使用 value = {numero} ?因为它只在屏幕上显示我的数字开头是什么(如果我确实启动了数字状态)
  • 我更新了我的答案,只是将 setNumero ({ numero: value }) 替换为 setNumero (value)
猜你喜欢
  • 2014-12-06
  • 1970-01-01
  • 2021-11-17
  • 2012-04-10
  • 2010-09-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-27
相关资源
最近更新 更多