I set up the theme provider from styled component, then I created a portal, and I elements from my portal cant access to the provider
This is my provider:
<ConfigProvider>
<ThemeProvider theme={myTheme}> //<-- provider from styled-components
<App />
</ThemeProvider>
</ConfigProvider>
This is how I created the portal:
export const SetSelector = ()=>{
const OptionsModal = useMemo(() => {
return (
<Wrapper $theme={configState.theme}>
<Modal $theme={configState.theme}>
// more components here, like buttons
</Modal>
</Wrapper>
);
}, [ . . . ]);
return (
<ListSelectorWrapper>
{displayOptions && createPortal(OptionsModal, document.body)}
</ListSelectorWrapper>
);
};
}
My components into my modal can't access to the theme provider from Styled Components, I got this error: Uncaught TypeError: Cannot read properties of undefined (reading '300')
which is from a property from my Button:
background: ${props=>props.$variant==="flat"?"transparent":props.$theme==="dark"?props.theme.gray[300]:props.theme.gray[600]};
Also, I tried Wrapping in a provider my portal but is not working, I got the same error
const OptionsModal = useMemo(() => {
return (
<ThemeProvider theme={myTheme}>
<WrapperOverlay $theme={configState.theme}>
<Modal $theme={configState.theme}>
// more components here, like buttons
</Modal>
</WrapperOverlay>
</ThemeProvider>
);
}, [...]);
How Can I access to theme provider values into my portal component?
Solved: was my mistake
I was not accessing in correct way to the values: props.theme.colors.gray[300]
instead props.theme.gray[300]
My way of using Styled-Components.
I use the ThemeProvider from Styled-Components as I would use the style variables with SASS.