Answer the question
In order to leave comments, you need to log in
How to add the required class to the parent from the Child component?
There is such markup
<div className="App">
<h1 className={preloader.wrapper}>
<Preloader />
Hello CodeSandbox
</h1>
<h2 className={preloader.wrapper}>
<Preloader />
Start editing to see some magic happen!
</h2>
</div>
Answer the question
In order to leave comments, you need to log in
This is not accepted, and you do not need to.
Since all of these elements must have the same class, they must be the root elements of the Preloader component instances. The tag can be passed via props, just like the elements that are now adjacent:
function Preloader({ Tag = 'h1', children }) {
return (
<Tag className={s.wrapper}>
<div className={s.preloader}></div>
{children}
</Tag>
);
}
<div className="App">
<Preloader>hello, world!!</Preloader>
<Preloader Tag="h2">fuck the world</Preloader>
</div>
Do it right like this:
<div className="App">
<Preloader>
<h1>
Hello CodeSandbox
</h1>
</Preloader>
<Preloader>
<h2>
Start editing to see some magic happen!
</h2>
</Preloader>
</div>
useLayoutEffect(() => {
const parent = ref.current.parentNode; // ref - корневой элемент
parent.classList.add(preloader.wrapper);
return () => {
parent.classList.remove(preloader.wrapper);
}
}, []);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question