Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/413.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何从我的主应用程序的文本输入(组件)中获取值?_Javascript_Android_Reactjs_React Native_Native - Fatal编程技术网

Javascript 如何从我的主应用程序的文本输入(组件)中获取值?

Javascript 如何从我的主应用程序的文本输入(组件)中获取值?,javascript,android,reactjs,react-native,native,Javascript,Android,Reactjs,React Native,Native,我的主应用程序带有一个文本、一个文本输入(一个组件)和一个按钮(另一个组件): 从“世博会状态栏”导入{StatusBar}; 从“React”导入React; 从“react native”导入{样式表、文本、视图、警报}; 从'./components/Tinput.js'导入{Tinput}; 从“./components/Button.js”导入{Button}; 导出默认函数App(){ 返回( 这是一辆公共汽车! ConsultarPokemon(/*这是我想插入Tinput中的值的

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

从“世博会状态栏”导入{StatusBar};
从“React”导入React;
从“react native”导入{样式表、文本、视图、警报};
从'./components/Tinput.js'导入{Tinput};
从“./components/Button.js”导入{Button};
导出默认函数App(){
返回(
这是一辆公共汽车!
ConsultarPokemon(/*这是我想插入Tinput中的值的地方*/)}>
安格尔
);
}
这是我的组件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 };
从“React”导入React;
从“react native”导入{TextInput};
常量输入=()=>{
常量[numero,setNumero]=React.useState(“”);
返回(
setNumero({numero:value})}
值={this.state.numero}
maxLength={20}
/>
);
}
出口{Tinput};
我想在onPress函数中使用文本输入的值,我尝试过这样做,但没有成功:

 <Button onPress={()=> ConsultarPokemon(Tinput.state.numero)}> 
        Ingresar 
 </Button> 
ConsultarPokemon(Tinput.state.numero)}>
安格尔
另外,我的Tinput组件上有一个错误:undefined不是对象(计算'\u this.state.numero')
因此,我可能也用错了state

只有在创建了组件类之类的类时,您才会使用这个.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>
      </>
    );
  }
}
类计数器扩展React.Component{
建造师(道具){
超级(道具);
this.initialCount=props.initialCount | | 0;
此.state={
count:这是initialCount
}
}
增量(){
this.setState({count:this.state.count+1})
}
重置(){
this.setState({count:this.initialCount})
}
render(){
const{count}=this.state;
返回(
+1
重置
计数:{Count}

); } }
我建议不要把事情复杂化,只需在罐头盒内按一下即可

    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>
      );
    }
consttinput=()=>{
常量[numero,setNumero]=React.useState(“”);
返回(
setNumero(值)}
value={numero}//无需使用this.state.numero
maxLength={20}
/>
口袋妖怪领事(头号)}>
安格尔
);
}

您无法访问其他类似的组件状态。您只能访问自己的组件状态,或访问树中较高级别组件的状态。因此,解决方案是移动使用这两个组件的组件中的状态。例如,我必须给应用程序一个数字状态,然后在我的Tinput组件上更改应用程序的状态?我怎样才能进入那个状态?app.setNumber({number:value})?谢谢,是的,我不应该把它做成一个组件。很抱歉再次打扰你,我把文本输入放在我的主应用上,所以它不再是一个组件了。但这不是我插入的值。当我在开始时启动numero状态时,它工作正常,但是(value)=>setNumero({numero:value})在我键入时不使用then input i insert。而且。。。是否需要使用值={numero}?因为它只在屏幕上显示开始时我的数字是什么(如果我启动了数字状态),所以我更新了我的答案,只是用setNumero(value)替换了setNumero({numero:value})
    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>
      );
    }