admin管理员组文章数量:1317906
I have the following scenario.
function MyComponent() {
return (
<View>
<TextInput ref={ref => (this.input = ref)} style={styles.input} />
{this.input.isFocused() && <Text>Hello World</Text>}
</View>
);
}
This actually works fine, but I get the warning:
MyComponent is accessing findNodeHandle inside its render. render should be a pure function.
How do I conditionally render The text block and avoid this warning?
I have the following scenario.
function MyComponent() {
return (
<View>
<TextInput ref={ref => (this.input = ref)} style={styles.input} />
{this.input.isFocused() && <Text>Hello World</Text>}
</View>
);
}
This actually works fine, but I get the warning:
MyComponent is accessing findNodeHandle inside its render. render should be a pure function.
How do I conditionally render The text block and avoid this warning?
Share Improve this question asked Feb 25, 2017 at 18:22 Steven ScaffidiSteven Scaffidi 2,3071 gold badge14 silver badges15 bronze badges2 Answers
Reset to default 5You can use ponent state :
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>
)
}
}
You cannot reference refs from the render() method. Read more about the cautions of working with refs
here.
As you can see in the image below, the first time the ponent mounts, refs is undefined, when I change the text (Which changes the State, which re-renders the ponent) refs is now available.
An optimal solution would be using state. I was going to post a solution but Freez already did. However, I am still posting this so you know why you should be using state instead of refs.
本文标签: javascriptReact Native conditionally render part of view while input is focusedStack Overflow
版权声明:本文标题:javascript - React Native conditionally render part of view while input is focused - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742008955a2412528.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论