Передать данные от ребёнка к ребёнку

Как передать данные от родителя к ребёнку, когда данные от другого ребёнка к родителю были получены?

а то null передаёт

<ButtonsSaveComponent settings={{difficulty, operators}}/>

import React, {useState} from 'react';
import DifficultyComponent from "../components/SettingsPagesComponents/DifficultyComponent";
import OperatorsCheckBoxesComponent from "../components/SettingsPagesComponents/OperatorsCheckBoxesComponent";
import ButtonsSaveComponent from "../components/SettingsPagesComponents/ButtonsSaveComponent";

const SettingsTest = () => {

    const [difficulty, setDifficulty] = useState(0);
    const [operators, setOperators] = useState({"plus": true, "minus": true, "multiply": true, "divide": true});

    

    const updateDataDifficulty = (data) => {
        setDifficulty(data);
    }

    const updateDataOperators = (data) => {
        setOperators(data);
    }

    return (
        <div>

            <div className="container">
                <div className="row">
                    <div className="col-md-12">
                        <h1>Настройки игры</h1>
                    </div>
                </div>
                <h1>{difficulty}</h1>
                <DifficultyComponent updateDataDifficulty={updateDataDifficulty}/>
                <OperatorsCheckBoxesComponent updateDataOperators={updateDataOperators}/>
                <ButtonsSaveComponent settings={{difficulty, operators}}/>
            </div>
        </div>
    );
};

export default SettingsTest;

ButtonsSaveComponent. Здесь я хочу получать данные от родителя, которому уже пришли другие данные от детей. Но сюда они приходят дефолтные, хотя если смотреть в родителе, то они изменились

import React, {useEffect, useState} from 'react';

const ButtonsSaveComponent = ( {settings} ) => {

    const [difficulty, setDifficulty] = useState();
    const [operators, setOperators] = useState();

    useEffect(() => {
        setDifficulty(settings.difficulty);
        setOperators(settings.operators);
    }, []);

    return (
};

export default ButtonsSaveComponent;

Ответы (1 шт):

Автор решения: EzioMercer
  1. useState() - положите туда начальное значение например null и в ButtonsSaveComponent пропишите начальные данные, если на вход придут null

  2. updateDataDifficulty бесполезен, если вы никак не обрабатываете входящий data, можете сразу передавать в дочерний элемент setDifficulty

→ Ссылка