Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is there a way to solve this problem , Type '{ children: Element; }' has no properties in common with type 'IntrinsicAttributes'.?

I create a file called ThemeContext:

import { createContext, useState } from "react";
import PropTypes from "prop-types";
import { DARK_THEME, LIGHT_THEME } from "../constants/themeConstants";

interface ThemeContextType {
  theme: string;
  toggleTheme: () => void;
}

export const ThemeContext = createContext<ThemeContextType>({
  theme: LIGHT_THEME,
  toggleTheme: () => {}
});

export const ThemeProvider: React.FC<{}> = ({ children }: any) => {
  const [theme, setTheme] = useState<string>(
    window.localStorage.getItem("themeMode") || LIGHT_THEME
  );

  window.localStorage.setItem("themeMode", theme);

  const toggleTheme = () => {
    setTheme((prevTheme) =>
      prevTheme === LIGHT_THEME ? DARK_THEME : LIGHT_THEME
    );
    window.localStorage.setItem("themeMode", theme);
  };

  return (
    <ThemeContext.Provider
      value={{
        theme,
        toggleTheme,
      }}
    >
      {children}
    </ThemeContext.Provider>
  );
};

ThemeProvider.propTypes = {
  children: PropTypes.node.isRequired,
};

and this is where I am using the theme provider :

import "./App.scss";
import { ThemeProvider } from "./context/ThemeContext";

import Admin from "./routes/admin/Admin";
import Global from "./routes/global/Global";

function App() {
  return (
    <div className="App">
      <Global />
      
      <ThemeProvider>
        <Admin></Admin>
     
      </ThemeProvider>
        
     
    </div>
  );
}

export default App;

but I got an error which is this:
Type ‘{ children: Element; }’ has no properties in common with type ‘IntrinsicAttributes’.ts(2559)
(alias) module ThemeProvider
(alias) const ThemeProvider: React.FC<{}>
import ThemeProvider

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I tried to understand the issue but it seems like the ThemeProvider doesn’ accept any element wrapped by it.
I hope someone help me

>Solution :

You have typed the ThemeProvider component to have no props, but are passing children props when wrapping components. The any type conflicts with the {} that was passed in React.FC<{}>.

Use the React.PropsWithChildren type.

export const ThemeProvider: React.FC<React.PropsWithChildren<{}>> = ({
   children
 }) => {
  ...
};

or just

export const ThemeProvider = ({ children }: React.PropsWithChildren<{}>) => {
  ...
};

If you had other component props they would be passed to PropsWithChildren.

interface ThemeProviderProps {
  someProp: string;
  someNumber: number;
}

export const ThemeProvider = (
  { children, someProp, someNumber }: React.PropsWithChildren<{ThemeProviderProps}>
) => {
  ...
};

You will also want to use an initializer function to set the initial theme state, and a useEffect hook to issue the side-effect to update localStorage when the theme state value changes.

Example:

import { PropsWithChildren } from 'react';

export const ThemeProvider = ({ children }: PropsWithChildren<{}>) => {
  const [theme, setTheme] = useState<string>(
    () => window.localStorage.getItem("themeMode") || LIGHT_THEME
  );

  useEffect(() => {
    window.localStorage.setItem("themeMode", theme);
  }, [theme]);

  ...
};
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading