How can I use a button to change the Theme for material UI on my site? this is where I am so far

Viewed 31

I am going to share my current code on code sandbox: https://codesandbox.io/s/gifted-mcnulty-6ejcx

I have attempted at creating a boolean and then using a button to change the state of that boolean which should in turn change the theme of the website. I would love some help in getting this to work and if you have any ideas on whether I should have multiple buttons for the themes I want to display or just 1 button which can cycle through more than 2 themes.

Any advice is much appreciated :)

1 Answers

From your code on codesandbox. Instead of

this.setState = { themeIs: false };

Because setState is a method, you have to write it like this

this.setState((state) => ({ themeIs: !state.themeIs }));

Live Demo

Edit Material-UI Swapping Theme

Related