In my parent component:
I have a boolean state:
const [isLoading, setIsLoading] = useState<boolean>(true);
Which i send to a child component:
return (
<OrganisationPersonsLister personsStripped={personsStripped} isLoading={isLoading} />
);
In my child component: i recieve the props
const OrganisationPersonsLister = React.memo((props: { isLoading: boolean }) => {
}
And depending on the boolean value i want it to render different components.
return (
<section className="org-person-lister">
{
(isLoading)
? <Spinner />
: <PopTable title={"title"} columns={columns} data={personsStripped} />
}
</section>
)
How i update the state in parent component:
const viewOrganisation = (orgId: string) => {
const org = orgList.find(o => o.id === orgId);
if (org !== undefined) {
setIsLoading(true)
setChosenOrg(org);
PersonApi.fetchPersonsByOrganisationId(orgId).then(response => {
console.log("fetchPersonByOrganisationId: ", response)
setIsLoading(false)
setPersonsStripped(response)
}).catch(err => {
console.error("Error occurred: ", err);
setIsLoading(false)
setPersonsStripped([])
});
}
};
The issue is that it does this once on initial load and then remains to false in child component even if the state is updated in the parent component. How can i make the state/props sync?
React.memo
. State in parent component is done through a axios function. – Arasto