简体   繁体   English

父组件 state 更改后子组件未重新渲染

[英]Child component not re rendering after Parent state changes

I have a RadioGroup component that contains multiple RadioButton components.我有一个包含多个RadioButton组件的RadioGroup组件。 Here's the code for the RadioGroup component:这是RadioGroup组件的代码:

const RadioGroup = ({radioGroupData}) => {
    const [radioGroupRefreshData, setRadioGroupRefreshData] = useState(radioGroupData);

    const handleClick = (index) => {
        setRadioGroupRefreshData(radioGroupRefreshData.map((obj, i) => {
            if(i !== index) {
                return {text: obj.text, isSelected: false};
            }
            return {text: obj.text, isSelected: true};
        }));
    };

    return (
        <View style={styles.container}>
            {
                radioGroupRefreshData.map((obj, i) => {
                    return <RadioButton index={i}
                                        text={obj.text}
                                        isSelected={obj.isSelected}
                                        onClick={handleClick} />
                })
            }
        </View>
    );
}

The RadioGroup component has a state variable (an array) called radioGroupRefreshData . RadioGroup组件有一个名为radioGroupRefreshData的 state 变量(一个数组)。 when each RadioButton is defined inside the RadioGroup, the handleClick function is passed as a prop in order to be called when a RadioButton is clicked.当每个 RadioButton 都在 RadioGroup 中定义时, handleClick function 作为道具传递,以便在单击 RadioButton 时调用。 Here is the code for the RadioButton component:这是RadioButton组件的代码:

const RadioButton = (props) => {
    const [isSelected, setIsSelected] = useState(props.isSelected);

    const initialRenderDone = useRef(false);
    useEffect(() => {
        if(!initialRenderDone.current) {
            initialRenderDone.current = true;
        }
        else {
            props.onClick(props.index);
        }
    }, [isSelected]);

    const handlePress = () => {
        if(!isSelected) {
            setIsSelected(true);
        }
    }

    return (
        <TouchableOpacity style={styles.outsideContainer} onPress={handlePress}>
            <View style={styles.radioButtonContainer}>
                { (isSelected) && <RadioButtonInnerIcon width={15} height={15} fill="#04004C" /> }
            </View>
            <Text style={styles.radioButtonText}>{props.text}</Text>
        </TouchableOpacity>
    );
}

From what I know, each RadioButton component should re render when the Parent's variable radioGroupRefreshData changes, but the RadioButton component's are not re rendering.据我所知,当 Parent 的变量radioGroupRefreshData更改时,每个 RadioButton 组件都应该重新呈现,但 RadioButton 组件不会重新呈现。

Thank you in advance for any help that you can give me!预先感谢您能给我的任何帮助!

Since you have a state in RadioButton you need to update it when the props change.由于您在 RadioButton 中有一个 state,因此您需要在道具更改时对其进行更新。 So in RadioButton add useEffect like this:所以在 RadioButton 添加 useEffect 像这样:

useEffect(() => {
    setIsSelected(props.isSelected);
},[props.isSelected]);

Also you don't have to mix controlled and uncontrolled behaviour of the component: do not set RadioButton state inside RadioButton since it comes from the RadioGroup此外,您不必混合组件的受控和不受控行为:不要在 RadioButton 内设置 RadioButton state,因为它来自 RadioGroup

you might need to provide key prop to RadioButton component您可能需要为RadioButton组件提供key prop

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM