简体   繁体   English

太多的重新渲染。 在 React Native 中

[英]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.

相关问题 太多的重新渲染。 React 限制渲染次数防止死循环 | 反应原生 - Too many re-renders. React limits the number of renders to prevent an infinite loop | React Native 太多的重新渲染。 React 限制渲染的数量 - Too many re-renders. React limits the number of renders 错误:重新渲染过多。 React 限制渲染次数 - Error: Too many re-renders. React limits the number of renders 错误:重新渲染太多。 对后端的帖子做出反应 - Error: Too many re-renders. react on post to backend CheckBoxes React Native Map 函数返回错误:重新渲染太多。 React 限制渲染次数以防止无限循环 - CheckBoxes React Native Map function returns error: Too many re-renders. React limits the number of renders to prevent an infinite loop React Native- 出现错误:重新渲染次数过多。 React 限制渲染次数以防止在显示模态时出现无限循环 - React Native- Getting Error: Too many re-renders. React limits the number of renders to prevent an infinite loop while showing modal 太多的重新渲染。 React 限制渲染次数以防止无限循环 - React hooks - Too many re-renders. React Limits the number of renders to prevent an infinite loop - React hooks 反应错误:重新渲染太多。 React 限制渲染次数以防止无限循环 - React Error: Too many re-renders. React limits the number of renders to prevent an infinite loop React js:- 重新渲染太多。 React 限制渲染次数以防止无限循环 - React js :- Too many re-renders. React limits the number of renders to prevent an infinite loop 反应:错误:重新渲染太多。 React 限制渲染次数以防止无限循环 - React: Error: Too many re-renders. React limits the number of renders to prevent an infinite loop
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM