Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { useContext } from "react";
import { ThemeContext } from "./Theme";

const Card = () => {
const { theme, toggleTheme } = useContext(ThemeContext);
const context = useContext(ThemeContext);
if(!context)
throw new Error("useTheme must be provided within a ThemeProvider")

const{theme,toggleTheme} =context

return (
<div
Expand Down Expand Up @@ -35,4 +38,4 @@ const Card = () => {
);
};

export default Card;
export default Card;

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@

import { createContext, ReactNode, useContext, useState } from "react";
import Card from "./Card";

type Theme = "light" | "dark"

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


export const ThemeContext = createContext<ThemeContextType | undefined>(undefined);

const ThemeProvider:React.FC<{children:ReactNode}> = ({ children }) => {
const [theme, setTheme] = useState<Theme>("light");

const toggleTheme = () => {
setTheme((prevTheme)=>prevTheme === "light" ? "dark" : "light");
};

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

const Theme = () => {
return (
<ThemeProvider>
<Card />
</ThemeProvider>
);
};


export const useTheme =():ThemeContextType=>{
const context =useContext(ThemeContext)
if(!context){
throw new Error("useTheme must be provided within a ThemeProvider")
}

return context
}

export default Theme;
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { use } from "react";
import { ThemeContext } from "./Theme";

const Card = () => {
// Now by using react 19, we don't have to use "useContext" we can replace that with "use"
const { theme, toggleTheme } = use(ThemeContext);
const context = use(ThemeContext);
if(!context)
throw new Error("useTheme must be provided within a ThemeProvider")

const{theme,toggleTheme} =context

return (
<div
Expand Down Expand Up @@ -36,4 +38,4 @@ const Card = () => {
);
};

export default Card;
export default Card;

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@

import { createContext, ReactNode, useContext, useState } from "react";
import Card from "./Card";

type Theme = "light" | "dark"

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


export const ThemeContext = createContext<ThemeContextType | undefined>(undefined);

const ThemeProvider:React.FC<{children:ReactNode}> = ({ children }) => {
const [theme, setTheme] = useState<Theme>("light");

const toggleTheme = () => {
setTheme((prevTheme)=>prevTheme === "light" ? "dark" : "light");
};

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

const Theme = () => {
return (
<ThemeProvider>
<Card />
</ThemeProvider>
);
};


export const useTheme =():ThemeContextType=>{
const context =useContext(ThemeContext)
if(!context){
throw new Error("useTheme must be provided within a ThemeProvider")
}

return context
}

export default Theme;