React Navigation - setOptions() headerRight 回调中的访问状态

2024-04-24

我凌驾于一切之上反应导航 https://reactnavigation.org, headerRight内部带有自定义按钮的选项React.useEffect。 当按下按钮时,我需要访问状态name但我得到的值不是当前的值。

const [name, setName] = React.useState('')

React.useEffect(() => {
  navigation.setOptions({
    headerRight: function HB() {
          return (
            <HeaderBtn onPress={submit}>
              Submit
            </HeaderBtn>
          )
        }
      : null,
  })
}, [])

function submit() {
  console.log(name) // ''
  // It always returns an empty string, instead of the
  // most recent name setted by TextInput.
  // How can I access the current name?
}

return (
  <TextInput
    nativeID="inputNameLabel"
    onChangeText={setName}
  />
)

有什么办法可以访问当前的name无需打电话setOptions()在每次重新渲染时? (又名添加name to the .useEffect数组依赖)


None

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

React Navigation - setOptions() headerRight 回调中的访问状态 的相关文章