How to call a function immediately - reactjs

I write my request in my parent Component with code below:
useEffect(() => {
// my request
}, [])
As we all know, useEffect of parent component will implement after child component, so this request will be implemented after all child components rendered.
How can I call my request immediately before my child component's useEffect?

The order of execution of useEffect in a simple parent child component like this
<Parent>
<Child />
</Parent>
is, child useEffect first and then parent's.
If you want child to wait for parent's useEffect, you can use a conditional to show something else till then:
const [showChild, setShowChild] = useState(false);
useEffect(() => {
/* other activies */
setShowChild(true);
});
return (
<>
<otherСomponents />
{showChild && <Child />
<otherСomponents / >
</>
);

Don't put you function into useEffect, just in the component body:
export default function YourComponent() {
// Your logic here
return (
...
);
}
Because your component is just a function too, your function will be called immediately.

Simply, put statement of parent useEffect to top of child component's useEffect.

Related

How to have a React child component call a function from a parent component

I have a parent component and a child component (as a separate component file). How would I go about calling a function in the parent component from the child component?
The child component is imported into the parent component and the parent component has a function that makes an API call. I would like the child component to be able to also reach into the parent and run the api call function.
I could post the code I have but it's simply making an API call in the parent and having the child component imported in the parent.
thanks for any help.
Provide the function as a property on the child element:
const Parent = () => {
const someFunction = () => { /*...*/ };
return <Child someProp={someFunction}/>;
}
const Child = ({ someProp }) => {
return (
<div onClick={() => someProp()}>
Hello World
</div>
);
}

Does React know not to re-render child when parent changes?

<Parent P={some_state} >
<Child />
<Parent />
If some_state changes, causing the Parent to re-render, does it know to leave Child alone?
React Memo
I think what you are looking for is React.memo. Memoizing a component tells the component not to change or render unless the props it cares about changes or it has it's own internal changes.
In your example, if someState changes in the <Parent /> the <Child /> will render again. To fix that we can memoize our child.
Example Implementation
// Child Component
import React, { useEffect } from "react";
const Child = () => {
useEffect(() => console.log("Child rendered")); // simple way of logging each time the component renders
return (
<span>
I take no props, I shouldn't update unless my local state changes
</span>
)
};
const MemomizedChild = React.memo(Child);
// Parent Component
const Parent = ({ someState }) => {
return (
<MemomizedChild />
)
};
// Parent of Parent Component
import { useEffect, useState } from "react";
const App = () => {
const [foobar, setFoobar] = useState("hello");
useEffect(() => {
setTimeout(() => setFoobar("world"), 5000)
}, []) // after 5 seconds change the state variable to "world"
return (
<Parent someState={foobar} />
)
}
Explanation
Here are the steps or chain of events in that example:
App component has a local state variable foobar (just to trigger a prop change in the Parent component)
After five seconds the Parent's someState will change
Normally would trigger a render from the Child
We memoized Child so we should only see that "Child rendered" console log once
If we use the non memoized Child component the "Child rendered" console log will fire twice, once on mount and the second time when the Parent props change.

React functional component communication

I'm working on a react webapp that requires a parent component call a function in the child component when a state changes. Something like this:
function Parent() {
const [var,setVar] = useState();
useEffect(() => {
//something to call doSomething()
},[var])
return <div> <Child /> </div>
}
function Child() {
const [data,setData] - useState([]);
doSomething() {
fetch('/path/').then((resp) => {
setData(resp.data)
}
}
return <div> Child </div>
}
This is actually my first time using react so sorry if this is a stupid question. I looked into it and it doesn't seem like contexts are the best to use for this case so I was wondering if there was an easier way to call "doSomething()". Thank you.
I am using the child in 2 places in my application. That is why I want to do this.
Easiest way for you to execute doSomething in a child is, if you pass in the dependent value that affects the function execution i.e. your parent component executes the function when var changes.
Pass in the var as prop to your child component and move the effect from parent to child component.
This would mean if something in parent component changes (in this case var changes), a function in child component gets executed.
You also can do this through the use of callbacks. Pass in a callback to the child component, then pass doSomething into said callback. You then have access to the function.
function Parent() {
const [var,setVar] = useState();
// State to contain the child's function
const [doSomething, setDoSomething] = useState()
// Callback to receive the child's function
const handleDoSomething = doSomething => setDoSomething(doSomething)
useEffect(() => {
doSomething()
},[var, doSomething])
// Pass the callback to Child
return <div> <Child getDoSomething={ handleDoSomething } /> </div>
}
function Child({ getDoSomething }) {
doSomething() {return "something"}
// Pass doSomething into the callback
getDoSomething(doSomething)
return <div> Child </div>
}

How to pass props from functional component to class component

I'm quite new to functional components and I've been on this for hours.
I simply want to pass a prop from a parent functional component to a class child component so that I will use the updated prop (which will be a boolean) to change the display of an element. That's all.
In this scenario, I want to switch the sidenav from opened to closed based on the prop.
Here is the parent component (functional):
let opened = false;
function openSidenav(){
opened = !opened;
}
const [sidebarOpened, setOpened ] = useState(opened);
return (
<Sidebar sidebarOpened={opened} />
)
and the child component (class):
componentDidUpdate(prevProps) {
if(this.props.sidebarOpened !== prevProps){
this.setState({ sidebar: true});
}
}
It's just not working, the child component isn't receiving changes, I guess I didn't pass the prop rightly. I know the code just needs correction but I don't know where I'm not getting it.
Thank you.
The argument to useState is only used once. You need to set the state in openSidenav function to trigger a re-render.
Parent
function openSidenav(){
setOpened(prev => !prev);
}
const [sidebarOpened, setOpened ] = useState(false);
return (
<Sidebar sidebarOpened={sidebarOpened} />
)
Also in child component, use prevProps.sidebarOpened (not just prevProps).
Child
componentDidUpdate(prevProps) {
if(this.props.sidebarOpened !== prevProps.sidebarOpened){ //<---- see here
this.setState({ sidebar: this.props.sidebarOpened});//<---- see here
}
}
p.s - Also, since you are using props directly in child component, you can consider to not to copy props into state.
In parent,
const [sidebarOpened, setOpened ] = useState(false);
function openSidenav(){
setOpened(!sidebarOpened);
}
return (
<Sidebar sidebarOpened={sidebarOpened} />
)
And in child component class directly use this.props.sidebarOpened instead of copying over the prop to state. If you intend to edit the value of sidebarOpened in the child component, pass setOpened to the child component as a prop and use that to edit the value.

Unmounting a component when triggered through parent component

I have a parent component and child component. Child component will be rendered through parent based on a condition. So, when child gets rendered, all useEffect and other code gets executed and I'm able to achieve componentDidMount kind of methods. I'm not directly accessing child component but only use Parent to render it based on parent's props.
Once my child component is visible, I call a method which resets 'doesChildExists' prop and re-render ParentComponent and since the condition is failed, ChildComponent is not rendered (unmounted). Once, ChildComponent is about to unmount, I need to do some stuff, but existing useEffect doesn't seem to get called. How do I handle this? Please help!!
Please, let me know if this is not the right approach, and I have handle rendering of child in a different way.
const ChildComponent: FunctionComponent<Props> = (props: Props) => {
useEffect(() => {
return () => {
// handle unmount
}
}, []);
return (
<div class='textDiv'>
This is content
</div>
)
}
const ParentComponent: FunctionComponent<ParentProps> = (props: ParentProps) => {
return (
<>
{doesChildExists &&
createPortal(<ChildComponent />, document.body)}
</>
)
}

Resources