小编Mar*_*ina的帖子

如何将 useState 更新为 TextInput

我<TextInput>在 React Native 中有一个简单的,我想在值改变时打印一些文本。它在输入第二个字符后工作,但当用户只按一个字符时则不工作。我尝试使用函数,甚至使用 onChange 并从 e.target.value 中获取,但在“搜索”状态下总是缺少一个字符。

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

const App = () => {
  const [search, setSearch] = useState('');
  const [searching, setSearching] = useState(false); 
  return(
    <View>
      <TextInput 
        onChangeText={(value) => {
          setSearch(value)
          setSearching(search=='' ? false : true)
        }}
        value = { search }
      />
      {
        searching ?
        (
          <Text>Searching</Text>
        ) : (
          <Text>No searching</Text>
        )
      }
    </View>
  );
} 

export default App
Run Code Online (Sandbox Code Playgroud)

我希望在 TextBox …

react-native react-hooks

2
推荐指数
1
解决办法
8303
查看次数

标签 统计

react-hooks ×1

react-native ×1