react-native - 在第二个函数中使用 setter 方法反应原生钩子状态变化
问题描述
我有 2 个函数,function1() 和 function2()
function2(){
return(
<TextInput/>
)
}
export default function1(){
const [name,setName]=useState('')
return(
<View>
{function2}
</View>
}
我在第二个函数中有一些文本输入,当用户输入某些内容时,我需要更改状态(在第一个函数中)。 如何处理这种情况?
解决方案
将处理函数作为 props 传递并在 function2 中调用它。
function2(props){
//wherever you get the input call
props.handleTextInput(inputString)
return(
<TextInput/>
)
}
export default function1(){
const [name,setName]=useState('');
const handleTextInput = (name) => {
setName(name);
}
return(
<View>
<function2 handleTextInput = {this.handleTextInput} />
</View>
);
}
推荐阅读
- python - 如何通过 websocket 连接?
- vulkan - vulkan 管道内存屏障是否缓解了与没有内存屏障的管道屏障相关的同步约束?
- javascript - 道具验证中缺少“道具”?
- gremlin - Gremlin hasId 使用父级的属性
- java - 如何在 Jenkins 管道作业构建中提供文件夹路径
- python - Python写入文件失败
- jenkins-pipeline - Jenkins Pipeline - 带有矩阵名称变量的 If/Else 语句
- javascript - 初始自动对焦不显示焦点波纹
- visual-studio-code - 在 VSCode 编辑器中更改/创建元素的范围
- c++ - 视觉工作室似乎坏了