强制文件在reactNative中重新渲染

问题描述 投票:0回答:1

函数

updateVerification(placeholder, true)
拒绝运行。

它应该在我打开屏幕时自动运行,但我注意到该函数仅在我重新保存文件时运行。从字面上看,我必须打开这个文件,然后重新保存它,该函数才能运行。

所以,我意识到这个文件必须在屏幕打开时自动重新渲染。

PS:我尝试更改运行该函数的条件。我设置了country.name > 0。我还尝试在

inputValue
的依赖项中设置
country
useEffect
,但没有任何效果。

所以我已将文件设置为自动重新渲染,还是我以错误的方式调用该函数?或者具体是什么?

export default function CountrySelect({ inputValue, placeholder, onChange,updateVerification }) {
    const [country, setCountry] = useState(inputValue || { cca2: 'AE', name: 'United Arab Emirates' });
    const handleSelect = (selectedCountry) => {
        setCountry(selectedCountry);
        onChange(selectedCountry.name);
    };
    useEffect(() => {
        updateVerification(placeholder, true);
    }, [])
    return (
        <>
            <Text style={styles.title}>{placeholder}</Text>
            <CountryPicker
                placeholder={placeholder}
                countryCode={country.cca2}
                onSelect={handleSelect} />
        </>
    );
};
javascript react-native expo
1个回答
0
投票

您必须将依赖项添加到

useEffect

    useEffect(() => {
        updateVerification(placeholder, true);
    }, [country]) // Add dependencies here to execute useEffect again
© www.soinside.com 2019 - 2024. All rights reserved.