fix(fs.dev): (hackish) workaround for theme prop update issue
This is a workaround for the following problem that I can't seem to understand. This JSX code: <p data-theme={props.app.theme}>{props.app.theme}</p> Is rendered as: <p data-theme="light">dark</p> This happens only upon initial page load because on the server side we don't know the user's preferred theme. So we start with the default (light). Then, as the app bootstraps in the browser, it updated the content of the P tag with the new theme (dark). However, for reasons that I really don't understand, it does not update the `data-theme` attribute. So this works around that be forcing a different component for each theme that has the data-theme hardcoded. By force-rendering a different component, we can be certain React has no choice be to re-render the entire tag. If anybody could explain to me why this happen, I'd gladly buy you a coffee or something.
This commit is contained in:
parent
1bc0b4985c
commit
bb901c4d8a
1 changed files with 13 additions and 7 deletions
|
@ -53,18 +53,24 @@ const AppWrapper= props => {
|
|||
// locale, languages: config.languages,
|
||||
//}
|
||||
|
||||
// Cannot understand why, but a re-render does update the content
|
||||
// but not the attributes. So this is a hackish workaround
|
||||
const themeWrappers = {
|
||||
light: props => <div {...props} data-theme="light" />,
|
||||
dark: props => <div {...props} data-theme="dark" />,
|
||||
hax0r: props => <div {...props} data-theme="hax0r" />,
|
||||
lgbtq: props => <div {...props} data-theme="lgbtq" />,
|
||||
trans: props => <div {...props} data-theme="trans" />,
|
||||
}
|
||||
const Wrapper = themeWrappers[props.app.theme]
|
||||
|
||||
return (
|
||||
<div
|
||||
{...swipeHandlers}
|
||||
data-theme={props.app.theme}
|
||||
key={props.app.theme}
|
||||
className={`theme-${props.app.theme}`}
|
||||
>
|
||||
<Wrapper>
|
||||
{props.noLayout
|
||||
? props.children
|
||||
: <Layout {...childProps}><p className={`theme-${props.app.theme}`} data-theme={props.app.theme}>{props.app.theme}</p>{props.children}</Layout>
|
||||
}
|
||||
</div>
|
||||
</Wrapper>
|
||||
)
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue