简体   繁体   English

React Native State 晚更新 1 个按键

[英]React Native State is updating 1 keypress late

I am making an autocomplete for an input.我正在为输入做一个自动完成。 I have an input which calls the queryText function onChangeText .我有一个调用queryText函数onChangeText的输入。 The queryText recieves a string and an array, it makes a new array with strings that match the passed string and updates state. queryText接收一个字符串和一个数组,它使用与传递的字符串匹配的字符串创建一个新数组并更新状态。 The issue is my state is updated late.问题是我的状态更新晚了。 In the code below if I type the letter "w" I get back the entire poi array instead of just walmart.在下面的代码中,如果我输入字母“w”,我会返回整个 poi 数组,而不仅仅是 walmart。 But if I type "b" after I get only walmart (I should get nothing).但是,如果我在只得到 walmart 之后输入“b”(我什么也得不到)。 If I erase "b" I get an empty array (I should get walmart).如果我擦除“b”,我会得到一个空数组(我应该得到 walmart)。 It seems like my autocomplete state is 1 keypress behind.似乎我的自动完成状态落后 1 个按键。

export default function IndoorForm() {
  const poi = ["test1", "test2", "test3", "walmart"]

  const [value, onChangeText] = React.useState('');
  const [autocomplete, setAutocomplete] = React.useState([...poi]);
  const [query, setQuery] = React.useState('');

  const queryText = (text, poi) => {
    let sanitizedText = text.toLowerCase();
    const queryResult = poi.filter(location => location.includes(sanitizedText) !== false);
    setAutocomplete([...queryResult]);
    onChangeText(text);
    console.log("-----New Text----")
    console.log(queryResult); //this is displaying the correct array
    console.log(text);
    console.log(autocomplete); //this value is 1 behind what is expected
  }

return (
          <TextInput
              key={autocomplete}
              style={styles.input}
              onChangeText={text => queryText(text, poi)}
              value={value}
          />
);
}

React native may batch multiple setState() calls into a single update for performance. React Native 可以将多个 setState() 调用批处理为单个更新以提高性能。 Because this.props and this.state may be updated asynchronously, you should not rely on their values for calculating the next state.因为 this.props 和 this.state 可能会异步更新,所以你不应该依赖它们的值来计算下一个状态。

This counter example will fail to update as expected:此反例将无法按预期更新:

// Wrong
this.setState({
  counter: this.state.counter + this.props.increment,
})

The preferred approach is to call setState() with function rather than object.首选方法是使用函数而不是对象调用 setState()。 That function will receive the previous state as the first argument, and the props at the time the update is applied as the second argument.该函数将接收先前的状态作为第一个参数,并将应用更新时的 props 作为第二个参数。

// Correct
this.setState((prevState, props) => ({
  counter: prevState.counter + props.increment
}))

in your case, you should also make setState() with function instead of object.在您的情况下,您还应该使用函数而不是对象来创建 setState()。 sometimes setstate() object is not immediately update and render.有时 setstate() 对象不会立即更新和呈现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM