Answer the question
In order to leave comments, you need to log in
How to create a component whose props will immediately fall into the props of all the underlying components?
Styled-components has a provider called ThemeProvider, it sets the theme={theme} prop. This prop is then directly available in all internal components. How to implement such a provider yourself?
Answer the question
In order to leave comments, you need to log in
Hello, some time ago I wrote an article on this topic on Habré, here is the link:
https://habr.com/ru/post/462963/
If you are using a react version higher than 16.8, replace theme-context/context.js in code on
import React from 'react';
const { Provider, Consumer } = React.createContext({});
export { Provider, Consumer };
camelCaseVlad , Your solution is almost exactly what you need, but you still have to use Consumer in your own components.
function MyComponent() {
const renderMyComponent = theme => {
return (
// code
);
};
return <ThemeConsumer>{theme => renderMyComponent(theme)}</ThemeConsumer>;
};
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question