gpt4 book ai didi

javascript - 当输入聚焦时,React Native 有条件地渲染部分 View

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:31:03 26 4
gpt4 key购买 nike

我有以下场景。

function MyComponent() {
return (
<View>
<TextInput ref={ref => (this.input = ref)} style={styles.input} />
{this.input.isFocused() && <Text>Hello World</Text>}
</View>
);
}

这实际上工作正常,但我收到警告:

MyComponent is accessing findNodeHandle inside its render. render should be a pure function.

如何有条件地呈现文本 block 并避免此警告?

最佳答案

你可以使用组件状态:

class MyComponent extends React.Component {

state = { isFocused: false }

handleInputFocus = () => this.setState({ isFocused: true })

handleInputBlur = () => this.setState({ isFocused: false })

render() {
const { isFocused } = this.state

return (
<View>
<TextInput
onFocus={this.handleInputFocus}
onBlur={this.handleInputBlur}
/>
{isFocused && <Text>Hello World</Text>}
</View>
)
}
}

关于javascript - 当输入聚焦时,React Native 有条件地渲染部分 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42459897/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com