The `className` property didn't align. Error encountered on the server while using React.js with Next.js

Hey there, I was trying to dynamically change the background and came up with this method. It's working fine but I'm encountering an error that is leaving me unsatisfied. Interestingly, when I hardcode a color, the error disappears. The code is functioning as expected, but the error persists.

function theme() {
const ls = require('local-storage');
const [dark, setdark] = useState(false);

function set(params) {
    setdark((dark) => !dark);
    if (dark) {
        console.log('disabled');
        ls.set('color', 'white');
    } else {
        console.log('enabled');
        ls.set('color', '#141526');
    }
}
const theme = ls.get('color');
return (
    <div>
        <style jsx global className='theme'>{`
            body {
                background: ${theme};
            }
        `}</style>
        <label htmlFor='dark'>Dark theme</label>
        <input type='checkbox' name='dark' id='' onClick={set} />
    </div>
);

}

export default theme;

Answer №1

Store the style tag in a constant variable to resolve the error...

incorrect --->

<div>
    <style jsx global className='theme'>{`
        body {
            background: ${theme};
        }
    `}</style>
    <label htmlFor='dark'>Dark theme</label>
    <input type='checkbox' name='dark' id='' onClick={set} />
</div>

correct --->

const style = (<style jsx global className='theme'>{`
        body {
            background: ${theme};
        }
    `}</style>)
<div>
    {style}
    <label htmlFor='dark'>Dark theme</label>
    <input type='checkbox' name='dark' id='' onClick={set} />
</div>

The issue was resolved by using it within a variable

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What could be causing the state object in React to not be updating correctly? There seems to be a discrepancy between the expanded and

Displayed on the console is a screenshot showing <br><br> I am working with React.js, and the object displayed in the image is an element within an array that is part of the state object. I'm puzzled by what's happening. The object a ...

Focusing on specific parents to target the same children using CSS and jQuery selectors

Is there a more efficient way to target specific children of select parents in CSS without excessive repetition and lengthening the selector? For instance, if I want to style the first-child paragraph within certain divs. The current CSS method: .funny ...

The HTML "details" element does not support repeated animations in Chrome

My goal is to animate elements within the summary section of an HTML details element. Interestingly, Firefox executes the animation perfectly and it repeats each time I open or close the details. However, on Chrome, the animation only runs when the details ...

What is the best way to eliminate all borders from a select box?

Is there a way to completely remove all borders from the selectbox using either CSS or JQuery? The code snippet is as follows: <select id="doctor_ch"> <option value="1" selected>One</option> <option value="2">Two</option& ...

Utilize props within a closure

When I put this.props.navigation.dispatch(resetAction); inside the componentWillMount() function, everything works smoothly. However, if I try to use this code snippet: componentWillMount(){ Keychain .getGenericPassword() .then(function(creden ...

It is not possible to make the event click work on the contents within a div

After conducting a sample chat, I am puzzled by the fact that the links or interactive buttons are not working within the content of the div. var ChatCompMap = new Map(); function ChatComponent(c, ident, userimg, username) { let id = c + '_&apo ...

Prevent a HTML button from being clicked multiple times before it disappears (using Django)

I am currently working on a Django project that involves events where users can join by adding their user_id and event_id to the attend model. On the event page, there is a join button form that, when clicked, adds the user to the attendees list. After cli ...

Sending default function parameters using Babel

Utilizing the stage-0 Babel preset has allowed me to incorporate expressions like myMethod = () => { /* code here */ } as class methods. However, upon implementing this feature with the stage-0 preset, I encountered issues with my projects not functioni ...

Using jQuery to modify a CSS property when a specific option is chosen

I'm currently working on creating an option dropdown menu and I've hit a roadblock trying to display a textbox only when the 'other' option is selected. Here's what I have so far: <span id="con-country"><label for="count ...

Special symbol for the homepage in React using Material UI design

I'm having trouble using the if statement to add different icons for each ListItem. React seems to be restricting me from doing so. Any suggestions on how I can achieve this? Should I make any changes in my approach? Thank you in advance. <List ...

Unable to change the disabled color of the Fab material in UI

Unable to customize the appearance of the disabled button. Attempted the solutions provided in this GitHub thread Material-UI version: "@material-ui/core": "3.8.1", "@material-ui/icons": "3.0.1", const styles = theme => ({ ...

Automated email formatting for a polished presentation

Aspiring to create automated emails upon user registration on my website, the aesthetic appeal of these emails is crucial to me. I have meticulously crafted an email template using HTML. You can view a snapshot here: https://i.sstatic.net/MpjfP.png Howev ...

Booting up a React component with ASP.NET

Looking for some help here...I have a solid collection of react components that I'm using in my asp.net 4.5 mvc 6 application with react.js, flux, gulp, and browserify. Everything works smoothly when my react components have all the necessary data. No ...

How can you ensure that an element returns to its original position after being animated in jQuery?

I want to add some animations to my website using jQuery, but I lack the skills to create them from scratch. I have managed to copy some code snippets, but I am facing an issue with one of my animations. When I click a button, the block animates, but I wan ...

Subdomain Dilemma: Trouble with Images in Internet Explorer

Currently, I am working on creating a basic website on a subdomain. Everything appears to be running smoothly in Google Chrome except for one issue with the header image not displaying in IE8. The image link is . It seems that the problem may stem from IE ...

There was a failure in executing cb() function, which was not called. This issue points to a problem within npm. To address this error, please make a report at

npm ERR! cb() function was never called! npm ERR! This issue originates from npm itself. To address, please file a report at: npm ERR! <https://github.com/npm/cli/issues> npm ERR! For a detailed log of this process, refer to ...

Center text within a div container

I am facing an issue with a nested div element: <div id="footer"> <div id="footer_text"> <p>My foooo text</p> </div> </div> The CSS code for the nested div is as ...

Adjust the position of divs in a row when there is overflow

I have a website with dynamic div boxes, and I want to organize them into rows with 4 boxes each using Bootstrap. It's working fine, but the issue arises when the text in a box is too long and creates a new line. Instead of moving down to accommodate ...

Determining When the Refetch Operation in React-Query Has Completed

In my current setup, I have a component that uses react-query to fetch data and set initial form values: const { data: caseData, refetch, isRefetching } = useQuery({ queryKey: `caseData-${caseId}`, queryFn: () => fetchCaseById(caseId), staleTime: ...

Is there a way to extract the content length from the raw DraftJS data?

I have a system where I am storing the data from my DraftJS editor in my database as a JSON string by passing it through convertToRaw(editorState.getCurrentContent()). For example, this is how the stored data looks like in the database: {"blocks": [{"key ...