[英]Too many re-renders. in React Native
I am making a custom TextInput component and in which i apply some different styles on the basis of state hook, which will be called onFocus and onBlur events, I've seen couple of solution on internet some of them are listed here Solution and i tried some of them but none of them work for me.我正在制作一个自定义 TextInput 组件,我在其中应用了一些不同的 styles 基于 state 钩子,这将被称为 onFocus 和 onBlur 事件,我在互联网上看到了几个解决方案,其中一些在此处列出其中一些,但没有一个对我有用。
NOTE: I am using Expo.注意:我使用的是 Expo。
Screen.js Screen.js
import InputField from '../Components/InputField'
const Screen = () => {
const [email, setEmail] = useState('')
const [password, setPassword] = useState('')
return(
<InputField
placeholder='user@example.com'
label='E-mail'
value={email}
setValue={setEmail()}
isSecure={false}
defState={false}/>
)
}
InputField.js InputField.js
const InputField = ({placeholder, label, value, setValue, isSecure, defState}) => {
const [isFocused, setFocus] = useState(!!defState)
const [isBlur, setBlur] = useState(!!defState)
const handle_focus = () => {
console.log('focused')
setFocus(true)
setBlur(false)
}
const handle_blur = () => {
console.log('blur')
setBlur(true)
setFocus(false)
}
return (
<View style={isBlur ? styles.container : styles.focusContainer}>
{isFocused ? <Text style={styles.label}>{label}</Text>: null}
<View style={styles.inputCont}>
<TextInput
placeholder={placeholder}
secureTextEntry={isSecure}
value={value}
onChangeText={setValue}
onFocus={()=>handle_focus}
onBlur={()=>handle_blur}
/>
<Icon name='checkmark-sharp' size={20} color={COLORS.blue} style={{marginLeft: 'auto'}}/>
</View>
</View>
);
}
Error:错误:
Error: Too many re-renders. React limits the number of renders to prevent an infinite loop.
In your InputField
change this在您的
InputField
中更改此
onFocus={()=>handle_focus}
onBlur={()=>handle_blur}
To this对此
onFocus={() => handle_focus()}
onBlur={() => handle_blur()}
And also, in your Screen
change this而且,在你的
Screen
上改变这个
setValue={setEmail()}
to This至此
setValue={(text) => setEmail(text)}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.