run function once but globally in react
when i make webpage, one thing i generally do is set all the divs to a random color using js for better understandng
now that i am using react. im confused how to do it
my react project goes like this
i have a
the
now i can write a function that that gets all the divs using
now one solution that i have is to make a
but that requires me to do the same thing multiple times for ALL the jsx files . is there any way i can basically call the function once somewhere globally and it'll effect all the jsx files so i dont need to repeat myself
or if there's any better way then I'm all in for it tnx
now that i am using react. im confused how to do it
my react project goes like this
i have a
Main.jsx that renders App.jsxApp.jsx has a main tag , inside which i have LeftSection and RightSection componentthe
LeftSection component has its own components that have their own jsx filenow i can write a function that that gets all the divs using
querySelectorAll() and loops through the arr and set random color to them, but when i make the function in App.jsx and call it there it only sets random color to the main tag cause App.jsx only has main tag in it inside which it has components. I thought since it has LeftSection and RightSection which have rest of the divs inside of them but ofc in their own component file, i thought the function might effect that as wellnow one solution that i have is to make a
Helper.js which will have have the coloring function expoted and i'll import it in all my jsx and run the functionbut that requires me to do the same thing multiple times for ALL the jsx files . is there any way i can basically call the function once somewhere globally and it'll effect all the jsx files so i dont need to repeat myself
or if there's any better way then I'm all in for it tnx


