<p>please login</p>
을 표시Navbar.jsx, NavLinks.jsx, UserContainer.jsx 생성
Navbar.jsx
import React, { useState } from 'react'
import NavLinks from './NavLinks'
const Navbar = () => {
const [user, setUser] = useState({name : 'bob'})
const logout = () => {
setUser(null)
}
return (
<nav className='navbar'>
<h5>CONTEXT API</h5>
<NavLinks user={user} logout={logout}/>
</nav>
)
}
export default Navbar
NavLinks.jsx
import React from 'react'
import UserContainer from './UserContainer'
const NavLinks = ({user, logout}) => {
return (
<div className='nav-container'>
<ul className="nav-links">
<li>
<a href="#">home</a>
</li>
<li>
<a href="#">about</a>
</li>
</ul>
<UserContainer user={user} logout={logout}/>
</div>
)
}
export default NavLinks
UserContainer.jsx
import React, { useState } from 'react'
const UserContainer = ({user, logout}) => {
return (
<div className='user-container'>
{user ? (
<>
<p>Hello There, {user?.name?.toUpperCase()}</p>
<button className='btn' onClick={logout}>
logout
</button>
</>
) : (
<p>Please Login</p>
)}
</div>
)
}
export default UserContainer
import React, { useState, createContext, useContext } from 'react'
import NavLinks from './NavLinks'
export const NavbarContext = createContext();
// custom hook
export const useAppContext = () => useContext(NavbarContext)
const Navbar = () => {
const [user, setUser] = useState({name : 'bob'})
const logout = () => {
setUser(null)
};
return (
<NavbarContext.Provider value={{user, logout}}>
<nav className='navbar'>
<h5>CONTEXT API</h5>
<NavLinks user={user} logout={logout}/>
</nav>
</NavbarContext.Provider>
)
}
export default Navbar
import React, { useContext, useState } from 'react'
import { NavbarContext, useAppContext } from './Navbar';
const UserContainer = () => {
const {user, logout} = useAppContext();
// const {user, logout} = useContext(NavbarContext);
return (
<div className='user-container'>
{user ? (
<>
<p>Hello There, {user?.name?.toUpperCase()}</p>
<button className='btn' onClick={logout}>
logout
</button>
</>
) : (
<p>Please Login</p>
)}
</div>
)
}
export default UserContainer
import React, { useContext } from 'react'
import UserContainer from './UserContainer'
import { NavbarContext, useAppContext } from './Navbar'
const NavLinks = () => {
const {user, logout} = useAppContext();
console.log(user)
return (
<div className='nav-container'>
<ul className="nav-links">
<li>
<a href="#">home</a>
</li>
<li>
<a href="#">about</a>
</li>
</ul>
<UserContainer user={user} logout={logout}/>
</div>
)
}
export default NavLinks
Context API를 사용하면 상태나 데이터를 상위 컴포넌트에서 하위 컴포넌트로 계속 props로 전달하지 않고 공유가능하다. 특히 계층 구조로된 컴포넌트 구조에서 데이터를 효율적으로 공유할 수 있다.
npm create vite@latest global-context -- --template react
cd global-context
npm install && npm run dev
context.js
import { createContext, useContext, useState } from "react";
const GlobalContext = createContext();
export const useGlobalContext = () => useContext(GlobalContext);
const AppContext = ({children}) => {
const [name, setName] = useState('JS');
return <GlobalContext.Provider value={{name, setName}}>
{children}
</GlobalContext.Provider>
}
export default AppContext;
main.js
import React from 'react'
import ReactDOM from 'react-dom/client'
import App from './App.jsx'
import './index.css'
import AppContext from './context.jsx'
ReactDOM.createRoot(document.getElementById('root')).render(
<React.StrictMode>
<AppContext>
<App />
</AppContext>
</React.StrictMode>,
)
App.jsx
import { useState } from 'react'
import reactLogo from './assets/react.svg'
import viteLogo from '/vite.svg'
import './App.css'
import { useGlobalContext } from './context'
function App() {
const [count, setCount] = useState(0)
const {name} = useGlobalContext();
console.log(name);
return (
<>
</>
)
}
export default App
GlobalContext를 전역에서 사용가능하도록 데이터를 담아서 사용한다.