Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

<Admin /> "theme" prop no longer updates/refreshes if providing a different "theme" conditionally. #8022

Closed
newfylox opened this issue Jul 29, 2022 · 5 comments
Labels

Comments

@newfylox
Copy link

What you were expecting:
In react-admin V3, I was able to update the theme conditionally and it was updating when rerendering.

What happened instead:
Now in react-admin V4 <Admin theme={theme} /> only keeps the first instance of theme, discarding other instances when rerendering.

Steps to reproduce:

Temporary solution
Again, in V3 it was working directly with <Admin theme={conditionalTheme} /> without effort. Now the only solution that I have is to separate <AdminContext /> and <AdminUI />, then give a first theme instance to <AdminContext /> and then update the theme with my condition inside <AdminUI /> using useTheme. And now it's working.

const theme = (condition) => {
  return !condition
    ? {
        ...defaultTheme,
        palette: {
          primary: green,
        },
      }
    : {
        ...defaultTheme,
        palette: {
          primary: yellow,
        },
      };
};

const App = (railsProps, railsContext) => {
  return () => {
    return (
      <BrowserRouter>
        <AdminContext
          dataProvider={dataProvider}
          authProvider={authProvider}
          i18nProvider={i18nProvider}
          theme={theme(false)}
        >
          <Resources />
        </AdminContext>
      </BrowserRouter>
    );
  };
};

const Resources = () => {
  const { permissions: initialPermissions } = usePermissions();
  const [permissions, setPermissions] = useState(initialPermissions);
  const [, setTheme] = useTheme();

  useEffect(() => {
    // dumb condition only to show my example, it could reset 2 or 3 times
    const condition = !permissions.something ? false : true;
    setTheme(theme(condition));
  }, [permissions]);

  return (
    <AdminUI layout={CustomLayout} loginPage={LoginPage}>
      {(updatedPermissions) => {
        setPermissions(updatedPermissions);
      }}
      <Resource name="example" />
    </AdminUI>
  );
};
@antoinefricker
Copy link
Contributor

Thanks for your report, I could reproduce the bug

@antoinefricker antoinefricker self-assigned this Aug 1, 2022
@fzaninotto
Copy link
Member

Note that if you want to change the theme programmatically, instead of doing it from outside react-admin, you can use the useTheme hook, as explained in the Theming documentation:

https://marmelab.com/react-admin/Theming.html#changing-the-theme-programmatically

@antoinefricker
Copy link
Contributor

You should note the bug fix on your sandbox using the react-admin 4.2.7. Consequently, I'll close the issue.

@newfylox
Copy link
Author

You should note the bug fix on your sandbox using the react-admin 4.2.7. Consequently, I'll close the issue.

Well I just tried updating to react-admin 4.2.7 but the button didn't change from green to yellow. Don't know if I missed something

@fzaninotto
Copy link
Member

Your CodeSandbox shows the right behavior with 4.2.7, so I think this is fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants