简体   繁体   中英

How to fix “Warning: Prop className did not match” when using UIkit and NextJs

The warning appears when using UIkit to style components in NextJS.

To reproduce:

  1. Create new nextjs app: npx create-next-app {app-name}
  2. cd {app-name} && npm i
  3. Replace the code in pages/index.js with the snippet below (2nd part)
  4. Create components/layout.js and add the snippet below (1st part)
  5. npm run dev and go to http://localhost:3000
  6. You'll see the warning in the console

The app functions normally, but the warnings are really a turn-off

 // components/layout.js (1st part) import Head from "next/head"; const Layout = ({ children }) => { return ( <> <Head> {/* UIkit CSS */} <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/uikit@3.5.5/dist/css/uikit.min.css" /> <title>NextJs App</title> {/* UIkit JS */} <script async src="https://cdn.jsdelivr.net/npm/uikit@3.5.5/dist/js/uikit.min.js" /> <script async src="https://cdn.jsdelivr.net/npm/uikit@3.5.5/dist/js/uikit-icons.min.js" /> </Head> {children} </> ); }; export default Layout; // pages/index.js (2nd part) import Layout from "../components/layout"; const Home = () => { return ( <Layout> <div className="uk-child-width-expand@s uk-text-center" data-uk-grid> <div> <div className="uk-card uk-card-default uk-card-body">Item</div> </div> <div> <div className="uk-card uk-card-default uk-card-body">Item</div> </div> <div> <div className="uk-card uk-card-default uk-card-body">Item</div> </div> </div> </Layout> ); }; export default Home;

The error on the console appears as shown in the picture below

在此处输入图像描述

The problem seems to have been the way I was loading the scripts.

When I changed the script attribute async to defer the Warning was gone and the page was compiled without any warnings

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM