How to update child state when button in parent component is clicked - reactjs

in child component I want to update the state when user clicked on button available in parent component and I've keep track of state value as this state is also affect by other code as well so I was thinking to use useEffect() hook but I'm not sure how to achieve it.
child component:
const [sentimentButtonValue, setSentimentButtonValue] = useState(false);
return(
<>
{sentimentButtonValue}
</>
)
parent Component:
const handelTableCardOpen = (idx) => {
// when this function call, want to update child 'sentimentButtonValue' state value
console.log(idx);
setSelectedRow(idx);
};
<Button key={idx} onClick={() => handelTableCardOpen (idx)}> Click </Button>

as others have stated, you need to life the state up, BUT JUST IN CASE you have a special case where you really need it
const Child = React.forwardRef((_, ref) => {
const [sentimentButtonValue, setSentimentButtonValue] = React.useState(false);
React.useImperativeHandle(ref, () => ({
whatEver: sentimentButtonValue,
setWhatEver: setSentimentButtonValue,
}));
return <>{sentimentButtonValue.toString()}</>;
});
const Parent = () => {
const childRef = React.useRef();
const handelTableCardOpen = () => {
childRef.current.setWhatEver(!childRef.current.whatEver);
};
return (
<>
<button onClick={handelTableCardOpen}>Click</button>
<Child ref={childRef} />
</>
);
};

You need to lift the state from the child to the parent component, and then pass that state as a prop to the child:
const Parent = () => {
const [sentimentButtonValue, setSentimentButtonValue] = useState(false)
const yourFunction = () => {
setSentimentButtonValue(newValue);
}
<Button
sentimentButtonValue={sentimentButtonValue}
onClick={yourFunction}
>
Click
</Button>
}

Related

how to pass dynamique data from child to parent in react native

i want to pass the data of text-input from child to parent to submit the dynamic form. when i use useEffect the phone blocked but i don't know why.please can someone help me to solve this problem.thanks to tell me if there are another way to pass the data.
child component
const RenderComponents = ({ sendChildToParent) => {
const [inputsVal, setInputsVal] = useState({});
const handleChange = (name, value) => {
setInputsVal({ ...inputsVal, [name]: value });
};
const senddata = () => {
sendChildToParent(inputsVal);
};
useEffect(senddata);
return (
<>
{getData.length === 0 ? (
<Empty />
) : (
getData.map((item, index) => {
switch (item.type) {
case "TextInput":
return (
<>
<InputText
onChangeText={(text) => handleChange(item.nameC, text)}
ModuleName={item.nameC}
placeholder={item.options.placeholder}
required={item.options.required}
key={index}
/>
</>
);
case "Phone":...
Parent Component
export function TemplateScreen(props) {
const navigation = useNavigation();
const [getData, setData] = React.useState(Mydata);
const [childData, setChildData] = useState([]);
const sendChildToParent = (dataFromChild) => {
setChildData(dataFromChild);
};
//*************************************Child Componenet*************** */
const RenderComponents = () => {
const [userTeam, setUserTeam] = useState({});
[...other code here...];
**********Parent Component*******
return (
<ScrollView>
<RenderComponents />
<Button
title="Submit"
onPress={()=>null}
/>...
The structure of your parent component is fine. The issues are in your child component, in the following lines:
const RenderComponents = ({ sendChildToParent) => {
const [inputsVal, setInputsVal] = useState({});
const handleChange = (name, value) => {
setInputsVal({ ...inputsVal, [name]: value });
};
const senddata = () => {
sendChildToParent(inputsVal);
};
useEffect(senddata);
it's not good practice to duplicate the input value in local state. Pass the value down from the parent component as well as the setter function.
you're not passing a dependency array to your useEffect function, so it runs on every render of the component. This sets off the following chain of events:
the parent renders
the child renders
useEffect runs, setting the value of the state in the parent
the parent re-renders
This is an endless loop and what causes your app to lock.
there's no need to wrap the state setting functions in your own functions unless you are planning to do additional work there later. There's also no need to run those functions in your component lifecycle (useEffect), because they will run when the input changes.
missing bracket in the first line.
You could rewrite the components in the following way:
// parent component
export function TemplateScreen(props) {
const navigation = useNavigation();
const [getData, setData] = React.useState(Mydata);
const [childData, setChildData] = useState({});
return (
<ScrollView>
<RenderComponents childData={childData} setChildData={setChildData} />
...
// child component
const RenderComponents = ({ childData, setChildData }) => {
const handleChange = (name, value) => {
setChildData({ ...childData, [name]: value });
};
return (
...

React useState update without set throught child component

I have a logic in my code that i don't understand, i can update a state (useState) throught my child components but without the "set" function.
Here is my (simplified) code :
const Main = () =>{
const [mission, setMission] = useState({activity:"", env:""})
const see = () =>{
console.log(mission)
}
return (
<Child1 data={mission} />
<button onClick={see}>TEST</button>
)
}
const Child1 = (props) =>{
const {data} = props
const [mission, setMission] = useState(data)
const handleChange = (value) =>{
mission["activity"] = value
}
return (
<Child2 data={mission} onChange={handleChange} />
)
}
const Child2 = (props) =>{
const {data} = props
const [activity, setActivity] = useState(data.activity)
const handleChange = (e) =>{
setActivity(e.target.value)
props.onChange(e.target.value)
}
return (
<input value={data} onChange={handleChange} />
)
}
I tried in sandbox and it work too, "mission" did update it's value throught all childs without any "setMission".
I'm relativily new to react so i miss something but i don't know what, can someone explain ?
Thank you
You probably want to "lift the state up". Only the Main component should useState. Then pass both the state value and update function to the child component. The child component will call this update function when it updates. This will update the state on the parent properly and re-render.
Using your example:
const Main = () => {
// Only one state at the top level
const [mission, setMission] = useState({ activity: "", env: "" });
const see = () => {
console.log(mission);
};
return (
<>
{/* Pass both the value and state update function to the child */}
<Child1 data={mission} update={setMission} />
<button onClick={see}>TEST</button>
<div>{mission.activity}</div>
</>
);
};
const Child1 = (props) => {
const { data, update } = props;
const handleChange = (e) => {
// This will set parent's state
update({ ...data, activity: e.target.value });
};
// You can follow the similar pattern to the next child
return <Child2 data={data} onChange={handleChange} />;
};
You can see a complete working example in this sandbox.

React initial state from props and any updates to local state should reflect in parent

What is the correct way to handle the case with React Hooks, where a child's state is set from parent prop, but any changes to the child should update the parent. The parent prop can keep changing.
For example,
const ParentComponent = () => {
const [container, setContainer] = useState({ name: 'hello'});
//some other user action causes container to change
//which should reflect in the child. Any subsequent update to child's input field
//should update parent again.
onChange = (payload) => {
container.name = payload.name
}
return (
<ChildComponent container={container} dispatch={onChange} />
);
};
const ChildComponent = (props) => {
const { container, dispatch } = props;
const [name, setName] = useState(container.name);
useEffect(()=>{
const payload = { name };
dispatch(payload);
},[name]);
return (
<input value={name} onChange= {(e)=>setName(e.target.value)} />
);
};
Would this not cause an infinite loop?

How do I pass this data from child to parent component?

I simply want to use a data that has been calculated in a child component and also use the same data in my parent component. How do i do so? This is my current approach, it works but i get this error:
Cannot update a component from inside the function body of a different component
Please suggest a correct method.
Parent:
const Parent = () => {
const [myValue, setMyValue] = useState("");
const getValue = (value) => {
setMyValue(value);
};
return (
<div className={styles.innerContainer}>
<div className={styles.introPara}>{myValue}</div>
<Child passToParent={getValue} />
</div>
);
};
export default Parent;
Child
const Child = ({ passToParent }) => {
let url = "www.google.com";
const findUrl = () => {
return url;
};
let finalValue = findUrl();
const finalText = finalValue.toUpperCase();
passToParent(finalText);
return (
<div>
<a href={finalValue}>
<div>{finalText}</div>
</a>
</div>
);
};
export default Child;
I want to use the finalText in both Child and Parent components but i don't want to repeat the logic i'm using in finalUrl( ). Currently i'm simply using passToParent( ) and it works but gives error.
try this - just pass myValue - instead getFunction:
getValue - this is the function - it's should be triggered on some user actions eg. onClick, onMouseEnter etc.
You hold myValue in useState reactHook - it's type: string
In child u destructurizing props - so there is option just to write it without processing.
Also there is possibility to pass function to child component
const Parent = () => {
const [myValue, setMyValue] = useState("");
const getValue = (value) => {
setMyValue(value);
};
return (
<div>
<Child passToParent={myValue} />
</div>
);
};
export default Parent;
const Child = ({ passToParent }) => {
return (
<div>
<a>
<div>{passToParent}</div>
</a>
</div>
);
};
export default Child;

State of child when parent calls useState update function

Suppose I have the following code snippet (Please consider it as a pseudo code)
Parent.js
const [state,action]=useState(0);
return <View><Child1/><Button onPress={()=>action(1)}/></View>
Child1.js
const [state]=useState(Math.random());
return <Text>{state}</Text>
So my question is when I click the button in the parent will the Chil1 state change or not.
On my local machine it seems it changes.
The benefit of useState is that once a component is mounted, the state value does not change across re-renders until the update state function is called, passing a new state value.
Therefore, even though your parent component Button press state change triggers a rerender of the child, since the child component is only being rerendered and not unmounted/remounted, the initial state of Math.random() would remain the same.
See useState in React: A complete guide
I don't know what exact scenario is, but if you just set default state, the state will be memorized like Scenario 1
Scenario 1
In this way, the state of Child will not be changed even if Parent re-render
const Child = () => {
const [state] = useState(Math.random());
return <div>{state}</div>
}
const Parent = () => {
const [, action] = useState(true);
return (
<>
<button onClick={() => action(false)}>Not Change</button>
<Child />
</>
);
}
Scenario 2
Unless you remove it and then re-render Parent even if memorize all Child, that is
const Child = () => {
const [state] = useState(Math.random());
return <div>{state}</div>
}
const Parent = () => {
const [state, action] = useState(true);
useEffect(() => {
if (!state) action(true)
}, [state])
return (
<>
<button onClick={() => action(false)}>Change</button>
{state && <Child />}
</>
);
}
Scenario 3
By the may, if you don't use default state, in this way, it will be changed every rendering like that
const Child = () => {
return <div>{Math.random()}</div>
}
const Parent = () => {
const [, action] = useState(true);
return (
<>
<button onClick={() => action(prev => !prev)}>Change</button>
<Child />
</>
);
}
Scenario 4
If we don't want Child to re-render, we can try memo to memorize it
const Child = memo(() => {
return <div>{Math.random()}</div>
})
Scenario 5
However, when Child has props, perhaps we should invole useCallback or useMemo to make sure the values or memory addresses of props are "fixed" like constant, so that Child won't re-render
(We don't have to use useCallback or useMemo all the time, it doesn't much matter when there is no performance problem)
const Child = memo((props) => {
return <div {...props}>{Math.random()}</div>
})
const Parent = () => {
const [, action] = useState(true);
const style = useMemo(() => ({}), [])
const onOK = useCallback(() => alert(1), [])
return (
<>
<button onClick={() => action(prev => !prev)}>Change</button>
<Child className="test" style={style} onClick={onOK} />
</>
);
}

Resources