import React from 'react'; import { createNextContext, useContextSelector } from '../../../src'; import './App.css'; interface Value { ball6: string; ball5: string; ball4: string; ball3: string; ball2: string; ball1: string; } const AppProvider = createNextContext({ ball1: 'red', ball2: 'green', ball3: 'red', ball4: 'green', ball5: 'red', ball6: 'green', }); const useBall1 = () => useContextSelector(AppProvider, (value) => value.ball1); const useBall2 = () => useContextSelector(AppProvider, (value) => value.ball2); const useBall3 = () => useContextSelector(AppProvider, (value) => value.ball3); const useBall4 = () => useContextSelector(AppProvider, (value) => value.ball4); const useBall5 = () => useContextSelector(AppProvider, (value) => value.ball5); const useBall6 = () => useContextSelector(AppProvider, (value) => value.ball6); const toggleColor = (oldValue: string) => oldValue === 'green' ? 'red' : 'green'; function App() { const [value, setValue] = React.useState({ ball1: 'red', ball2: 'green', ball3: 'red', ball4: 'green', ball5: 'red', ball6: 'green', }); const handleClick = () => { setValue({ ball1: toggleColor(value.ball1), ball2: value.ball2, ball3: toggleColor(value.ball3), ball4: value.ball4, ball5: toggleColor(value.ball5), ball6: value.ball6, }); }; const handleOddClick = () => { setValue({ ball1: value.ball1, ball2: toggleColor(value.ball2), ball3: value.ball3, ball4: toggleColor(value.ball4), ball5: value.ball5, ball6: toggleColor(value.ball6), }); }; return ( ); } function AppContent() { return (
); } const AppContentMemo = React.memo(AppContent); function Ball1() { const ball1Color = useBall1(); return
; } function Ball2() { const ball2Color = useBall2(); return
; } function Ball3() { const ball3Color = useBall3(); return
; } function Ball4() { const ball4Color = useBall4(); return
; } function Ball5() { const ball5Color = useBall5(); return
; } function Ball6() { const ball6Color = useBall6(); return
; } export default App;