Skip to content

Commit

Permalink
useContext
Browse files Browse the repository at this point in the history
  • Loading branch information
ken88ling committed Feb 15, 2022
1 parent 7438c56 commit c9e64ab
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
10 changes: 8 additions & 2 deletions host/src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import ReactDOM from 'react-dom';
import Header from 'nav/Header';

import './index.css';
import { CountProvider, useCount } from './store';

const App = () => {
const [count, setCount] = useState(0);
const [count, setCount] = useCount(0);

return (
<div className='container'>
Expand All @@ -18,4 +19,9 @@ const App = () => {
</div>
);
};
ReactDOM.render(<App />, document.getElementById('app'));
ReactDOM.render(
<CountProvider>
<App />
</CountProvider>,
document.getElementById('app')
);
15 changes: 15 additions & 0 deletions host/src/store.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import React, { createContext, useContext, useState } from 'react';

const CountContext = createContext([0, () => {}]);

export function CountProvider({ children }) {
return (
<CountContext.Provider value={useState(0)}>
{children}
</CountContext.Provider>
);
}

export function useCount() {
return useContext(CountContext);
}

0 comments on commit c9e64ab

Please sign in to comment.