-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(useSlice.ts): 🤖options naming, example
- Loading branch information
Showing
10 changed files
with
193 additions
and
145 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 0 additions & 25 deletions
25
...mponents/ExampleSection/components/ContextSection/context/hooks/useExamplePageHandlers.ts
This file was deleted.
Oops, something went wrong.
27 changes: 0 additions & 27 deletions
27
.../components/ExampleSection/components/ContextSection/context/hooks/useExamplePageState.ts
This file was deleted.
Oops, something went wrong.
38 changes: 38 additions & 0 deletions
38
...odal/components/ExampleSection/components/ContextSection/context/hooks/useExampleSlice.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { useSlice } from '@/hooks/useSlice'; | ||
|
||
import { createSlice } from '@/utils/react/create-slice'; | ||
|
||
type GlobalStateType = { | ||
value: number; | ||
}; | ||
|
||
const initialState: GlobalStateType = { | ||
value: 0, | ||
}; | ||
|
||
export const exampleSlice = createSlice({ | ||
initialState, | ||
reducers: { | ||
RESET: () => initialState, | ||
SET_VALUE: (state, payload: number) => { | ||
state.value = payload; | ||
}, | ||
INCREASE_VALUE: (state, payload: number) => { | ||
state.value += payload; | ||
}, | ||
DECREASE_VALUE: (state, payload: number) => { | ||
state.value -= payload; | ||
}, | ||
}, | ||
}); | ||
|
||
/** | ||
* 지역 컨텍스트에선 isGlobalSlice 옵션을 사용하지 않는것을 권장합니다. | ||
*/ | ||
export const useExampleAccessAbleSlice = () => { | ||
return useSlice(exampleSlice, { access: 'global' }); | ||
}; | ||
|
||
export const useExampleSlice = () => { | ||
return useSlice(exampleSlice); | ||
}; |
17 changes: 12 additions & 5 deletions
17
...odal/components/ExampleSection/components/ContextSection/context/useExamplePageContext.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,10 @@ | ||
import { Box, Button, Text } from '@chakra-ui/react'; | ||
|
||
import { homeSlice } from '@/contexts/pages/home/hooks/useHomePageState'; | ||
import { useHomePageContext } from '@/contexts/pages/home/useHomePageContext'; | ||
import { Box, Text } from '@chakra-ui/react'; | ||
|
||
function Home() { | ||
console.log('hi'); | ||
return ( | ||
<Box> | ||
<Text>HomePage</Text> | ||
<Count /> | ||
<Updator /> | ||
</Box> | ||
); | ||
} | ||
export default Home; | ||
|
||
const Count = () => { | ||
const count = useHomePageContext((ctx) => ctx.state.value); | ||
return ( | ||
<Box> | ||
<Text>{count}</Text> | ||
</Box> | ||
); | ||
}; | ||
|
||
const Updator = () => { | ||
const dispatch = useHomePageContext((ctx) => ctx.dispatch); | ||
|
||
return ( | ||
<Box> | ||
<Button | ||
onClick={() => { | ||
homeSlice.setState((prev) => ({ ...prev, value: prev.value + 1 })); | ||
}} | ||
> | ||
add | ||
</Button> | ||
|
||
<Button | ||
onClick={() => | ||
dispatch({ | ||
type: 'SET_VALUE', | ||
payload: 4, | ||
}) | ||
} | ||
> | ||
add2 | ||
</Button> | ||
</Box> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.