React Context와 글로벌 데이터 공유
1. 폴더 생성 및 react 설치
// type following commands in CLI
mkdir react-context
cd react-context
npm create-react-app client
cd client
code .
npm start
2. 폴더 구조 및 컨텍스트 파일 생성
모든 컨텍스트는 컨텍스트 폴더에 있으므로 이 폴더를 만든 것입니다.
src/
context/
ThemeContext.js
App.js
index.js
3. ThemeContext 파일을 열고 다음 코드를 붙여넣습니다.
import React, { useState } from "react";
/**
1. We create context with React.createContext({defaultValue})
2. We store created context in the variable
3. We export the context so that any component in component tree that context wraps can access it's data by the context variable
*/
export const ThemeContext = React.createContext({ themeMode: "light" });
const ThemeContextProvider = ({ children }) => {
const [themeMode, setThemeMode] = useState("light");
const toggleThemeMode = () => {
if (themeMode === "light") {
setThemeMode("dark");
} else {
setThemeMode("light");
}
};
/**
1. Context has Provider also Consumer but here it Provides data in the value attribute to component tree that it wraps.
2. Here we are sharing toggleTheemMode function that changes themeMode state from light to dark and vice versa. And themeMode with component tree
3. Children that Context wraps is component tree that ThemeContextProvider wraps and makes toggleThemeMode and themeMode available to them
*/
return (
<ThemeContext.Provider value={{ toggleThemeMode, themeMode }}>
{children}
</ThemeContext.Provider>
);
};
export default ThemeContextProvider;
4. index.js를 열고 다음 코드를 붙여넣습니다.
import React from "react";
import ReactDOM from "react-dom/client";
import ThemeContextProvider from "./ThemeContext";
import "./index.css";
import App from "./App";
/**
1. We've imported ThemeContextProvider function from ThemeContext
2. In here we're wrapping App root component and which is assigned to children prop and passed as argument to ThemeContextProvider function
3. As App is the root component, all the components regardless of the the level they are in the component tree, they can access the data ThemeContextProvider is providing
*/
const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(
<ThemeContextProvider>
<App />
</ThemeContextProvider>
);
5. 앱 파일을 열고 다음 코드를 붙여넣습니다.
import React, { useContext } from "react";
import { ThemeContext } from "./ThemeContext";
const App = () => {
const { themeMode, toggleThemeMode } = useContext(ThemeContext);
/**
1. We've imported the context ThemeContext that we'r created which makes global data available to use
2. We've also imported useContext hook to which we pass ThemeContext context and it returns use data that we've provided in that ThemeContext.Provider' value attribute
3. We've destructured themeMode and toggleThemeMode properties to use in this component
4. Based on themeMode value we're changing the background theme of the app
5. When button is clicked, we're calling toggleThemeMode which changes themeMode to light to dark or vice versa
*/
return (
<div
style={
themeMode === "light"
? { background: "#fff", height: "100vh" }
: { background: "#000", color: "#fff", height: "100vh" }
}
>
<button onClick={toggleThemeMode}>
{themeMode === "light" ? "Dark Mode" : "Light Mode"}
</button>
</div>
);
};
export default App;
Reference
이 문제에 관하여(React Context와 글로벌 데이터 공유), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://dev.to/johongirr/global-data-sharing-with-react-context-4ihi
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
// type following commands in CLI
mkdir react-context
cd react-context
npm create-react-app client
cd client
code .
npm start
모든 컨텍스트는 컨텍스트 폴더에 있으므로 이 폴더를 만든 것입니다.
src/
context/
ThemeContext.js
App.js
index.js
3. ThemeContext 파일을 열고 다음 코드를 붙여넣습니다.
import React, { useState } from "react";
/**
1. We create context with React.createContext({defaultValue})
2. We store created context in the variable
3. We export the context so that any component in component tree that context wraps can access it's data by the context variable
*/
export const ThemeContext = React.createContext({ themeMode: "light" });
const ThemeContextProvider = ({ children }) => {
const [themeMode, setThemeMode] = useState("light");
const toggleThemeMode = () => {
if (themeMode === "light") {
setThemeMode("dark");
} else {
setThemeMode("light");
}
};
/**
1. Context has Provider also Consumer but here it Provides data in the value attribute to component tree that it wraps.
2. Here we are sharing toggleTheemMode function that changes themeMode state from light to dark and vice versa. And themeMode with component tree
3. Children that Context wraps is component tree that ThemeContextProvider wraps and makes toggleThemeMode and themeMode available to them
*/
return (
<ThemeContext.Provider value={{ toggleThemeMode, themeMode }}>
{children}
</ThemeContext.Provider>
);
};
export default ThemeContextProvider;
4. index.js를 열고 다음 코드를 붙여넣습니다.
import React from "react";
import ReactDOM from "react-dom/client";
import ThemeContextProvider from "./ThemeContext";
import "./index.css";
import App from "./App";
/**
1. We've imported ThemeContextProvider function from ThemeContext
2. In here we're wrapping App root component and which is assigned to children prop and passed as argument to ThemeContextProvider function
3. As App is the root component, all the components regardless of the the level they are in the component tree, they can access the data ThemeContextProvider is providing
*/
const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(
<ThemeContextProvider>
<App />
</ThemeContextProvider>
);
5. 앱 파일을 열고 다음 코드를 붙여넣습니다.
import React, { useContext } from "react";
import { ThemeContext } from "./ThemeContext";
const App = () => {
const { themeMode, toggleThemeMode } = useContext(ThemeContext);
/**
1. We've imported the context ThemeContext that we'r created which makes global data available to use
2. We've also imported useContext hook to which we pass ThemeContext context and it returns use data that we've provided in that ThemeContext.Provider' value attribute
3. We've destructured themeMode and toggleThemeMode properties to use in this component
4. Based on themeMode value we're changing the background theme of the app
5. When button is clicked, we're calling toggleThemeMode which changes themeMode to light to dark or vice versa
*/
return (
<div
style={
themeMode === "light"
? { background: "#fff", height: "100vh" }
: { background: "#000", color: "#fff", height: "100vh" }
}
>
<button onClick={toggleThemeMode}>
{themeMode === "light" ? "Dark Mode" : "Light Mode"}
</button>
</div>
);
};
export default App;
Reference
이 문제에 관하여(React Context와 글로벌 데이터 공유), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://dev.to/johongirr/global-data-sharing-with-react-context-4ihi
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
import React, { useState } from "react";
/**
1. We create context with React.createContext({defaultValue})
2. We store created context in the variable
3. We export the context so that any component in component tree that context wraps can access it's data by the context variable
*/
export const ThemeContext = React.createContext({ themeMode: "light" });
const ThemeContextProvider = ({ children }) => {
const [themeMode, setThemeMode] = useState("light");
const toggleThemeMode = () => {
if (themeMode === "light") {
setThemeMode("dark");
} else {
setThemeMode("light");
}
};
/**
1. Context has Provider also Consumer but here it Provides data in the value attribute to component tree that it wraps.
2. Here we are sharing toggleTheemMode function that changes themeMode state from light to dark and vice versa. And themeMode with component tree
3. Children that Context wraps is component tree that ThemeContextProvider wraps and makes toggleThemeMode and themeMode available to them
*/
return (
<ThemeContext.Provider value={{ toggleThemeMode, themeMode }}>
{children}
</ThemeContext.Provider>
);
};
export default ThemeContextProvider;
import React from "react";
import ReactDOM from "react-dom/client";
import ThemeContextProvider from "./ThemeContext";
import "./index.css";
import App from "./App";
/**
1. We've imported ThemeContextProvider function from ThemeContext
2. In here we're wrapping App root component and which is assigned to children prop and passed as argument to ThemeContextProvider function
3. As App is the root component, all the components regardless of the the level they are in the component tree, they can access the data ThemeContextProvider is providing
*/
const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(
<ThemeContextProvider>
<App />
</ThemeContextProvider>
);
5. 앱 파일을 열고 다음 코드를 붙여넣습니다.
import React, { useContext } from "react";
import { ThemeContext } from "./ThemeContext";
const App = () => {
const { themeMode, toggleThemeMode } = useContext(ThemeContext);
/**
1. We've imported the context ThemeContext that we'r created which makes global data available to use
2. We've also imported useContext hook to which we pass ThemeContext context and it returns use data that we've provided in that ThemeContext.Provider' value attribute
3. We've destructured themeMode and toggleThemeMode properties to use in this component
4. Based on themeMode value we're changing the background theme of the app
5. When button is clicked, we're calling toggleThemeMode which changes themeMode to light to dark or vice versa
*/
return (
<div
style={
themeMode === "light"
? { background: "#fff", height: "100vh" }
: { background: "#000", color: "#fff", height: "100vh" }
}
>
<button onClick={toggleThemeMode}>
{themeMode === "light" ? "Dark Mode" : "Light Mode"}
</button>
</div>
);
};
export default App;
Reference
이 문제에 관하여(React Context와 글로벌 데이터 공유), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://dev.to/johongirr/global-data-sharing-with-react-context-4ihi
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
import React, { useContext } from "react";
import { ThemeContext } from "./ThemeContext";
const App = () => {
const { themeMode, toggleThemeMode } = useContext(ThemeContext);
/**
1. We've imported the context ThemeContext that we'r created which makes global data available to use
2. We've also imported useContext hook to which we pass ThemeContext context and it returns use data that we've provided in that ThemeContext.Provider' value attribute
3. We've destructured themeMode and toggleThemeMode properties to use in this component
4. Based on themeMode value we're changing the background theme of the app
5. When button is clicked, we're calling toggleThemeMode which changes themeMode to light to dark or vice versa
*/
return (
<div
style={
themeMode === "light"
? { background: "#fff", height: "100vh" }
: { background: "#000", color: "#fff", height: "100vh" }
}
>
<button onClick={toggleThemeMode}>
{themeMode === "light" ? "Dark Mode" : "Light Mode"}
</button>
</div>
);
};
export default App;
Reference
이 문제에 관하여(React Context와 글로벌 데이터 공유), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://dev.to/johongirr/global-data-sharing-with-react-context-4ihi텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)