-
Notifications
You must be signed in to change notification settings - Fork 5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add hook. Add local state. Add global state with duck and selectors. Add context state with context and hook.
- Loading branch information
1 parent
d3d81ad
commit 2c386a7
Showing
11 changed files
with
262 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import React, { useCallback, useState } from 'react'; | ||
import { | ||
AlignItems, | ||
BackgroundColor, | ||
IconColor, | ||
TextAlign, | ||
TextVariant, | ||
} from '../../../helpers/constants/design-system'; | ||
import { | ||
Button, | ||
ButtonIcon, | ||
ButtonIconSize, | ||
IconName, | ||
Text, | ||
} from '../../component-library'; | ||
import { Content, Header, Page } from '../../multichain/pages/page'; | ||
import { DEFAULT_ROUTE } from '../../../helpers/constants/routes'; | ||
import { useHistory } from 'react-router-dom'; | ||
import { useI18nContext } from '../../../hooks/useI18nContext'; | ||
import { useSample } from '../../../hooks/sample/useSample'; | ||
import { useSampleContext } from '../../../hooks/sample/useSampleContext'; | ||
|
||
export function SamplePage() { | ||
const t = useI18nContext(); | ||
const history = useHistory(); | ||
const [localState, setLocalState] = useState<number>(0); | ||
const { globalCounter, updateGlobalCounter } = useSample(); | ||
|
||
const { counter: contextCounter, updateCounter: updateContextCounter } = | ||
useSampleContext(); | ||
|
||
const handleLocalStateClick = useCallback(() => { | ||
setLocalState((prev) => prev + 1); | ||
}, [setLocalState]); | ||
|
||
const handleGlobalStateClick = useCallback(() => { | ||
updateGlobalCounter(1); | ||
}, [updateGlobalCounter]); | ||
|
||
const handleContextStateClick = useCallback(() => { | ||
updateContextCounter(1); | ||
}, [updateContextCounter]); | ||
|
||
return ( | ||
<Page className="main-container" data-testid="sample-page"> | ||
<Header | ||
backgroundColor={BackgroundColor.backgroundDefault} | ||
startAccessory={ | ||
<ButtonIcon | ||
ariaLabel={t('back')} | ||
iconName={IconName.ArrowLeft} | ||
className="connections-header__start-accessory" | ||
color={IconColor.iconDefault} | ||
onClick={() => history.push(DEFAULT_ROUTE)} | ||
size={ButtonIconSize.Sm} | ||
/> | ||
} | ||
> | ||
<Text | ||
as="span" | ||
variant={TextVariant.headingMd} | ||
textAlign={TextAlign.Center} | ||
> | ||
Sample Page | ||
</Text> | ||
</Header> | ||
<Content alignItems={AlignItems.center} gap={2}> | ||
<Text marginBottom={1}>{`Local React State: ${localState}`}</Text> | ||
<Button | ||
onClick={handleLocalStateClick} | ||
textAlign={TextAlign.Center} | ||
marginBottom={6} | ||
> | ||
Update Local State | ||
</Button> | ||
<Text marginBottom={1}>{`Global Redux State: ${globalCounter}`}</Text> | ||
<Button | ||
onClick={handleGlobalStateClick} | ||
textAlign={TextAlign.Center} | ||
marginBottom={6} | ||
> | ||
Update Redux State | ||
</Button> | ||
<Text marginBottom={1}>{`React Context State: ${contextCounter}`}</Text> | ||
<Button | ||
onClick={handleContextStateClick} | ||
textAlign={TextAlign.Center} | ||
marginBottom={6} | ||
> | ||
Update Context State | ||
</Button> | ||
</Content> | ||
</Page> | ||
); | ||
} |
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
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 @@ | ||
export type SampleGlobalState = { | ||
counter: number; | ||
}; | ||
|
||
type UpdateSampleCounterAction = { | ||
type: 'UPDATE_SAMPLE_COUNTER'; | ||
amount: number; | ||
}; | ||
|
||
type Action = UpdateSampleCounterAction; | ||
|
||
const INIT_STATE: SampleGlobalState = { | ||
counter: 0, | ||
}; | ||
|
||
export default function sampleReducer( | ||
// eslint-disable-next-line @typescript-eslint/default-param-last | ||
state: SampleGlobalState = INIT_STATE, | ||
action: Action, | ||
) { | ||
switch (action.type) { | ||
case 'UPDATE_SAMPLE_COUNTER': | ||
return { | ||
...state, | ||
counter: state.counter + action.amount, | ||
}; | ||
|
||
default: | ||
return state; | ||
} | ||
} | ||
|
||
export function updateSampleCounter(amount: number): UpdateSampleCounterAction { | ||
return { | ||
type: 'UPDATE_SAMPLE_COUNTER', | ||
amount, | ||
}; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { useCallback } from 'react'; | ||
import { SAMPLE_ROUTE } from '../../helpers/constants/routes'; | ||
import { useHistory } from 'react-router-dom'; | ||
import { useDispatch, useSelector } from 'react-redux'; | ||
import { selectSampleCounter } from '../../selectors/sample'; | ||
import { updateSampleCounter } from '../../ducks/sample/sample'; | ||
|
||
export function useSample() { | ||
const history = useHistory(); | ||
const dispatch = useDispatch(); | ||
const globalCounter = useSelector(selectSampleCounter); | ||
|
||
const updateGlobalCounter = useCallback( | ||
(amount: number) => { | ||
dispatch(updateSampleCounter(amount)); | ||
}, | ||
[dispatch], | ||
); | ||
|
||
const openSampleFeature = useCallback(() => { | ||
history.push(SAMPLE_ROUTE); | ||
}, [history]); | ||
|
||
return { globalCounter, openSampleFeature, updateGlobalCounter }; | ||
} |
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,48 @@ | ||
import React, { | ||
ReactElement, | ||
createContext, | ||
useCallback, | ||
useContext, | ||
useMemo, | ||
useState, | ||
} from 'react'; | ||
|
||
export type SampleContexType = { | ||
counter: number; | ||
updateCounter: (amount: number) => void; | ||
}; | ||
|
||
export const SampleContext = createContext<SampleContexType | undefined>( | ||
undefined, | ||
); | ||
|
||
export function SampleContextProvider({ children }: { children: ReactElement }) { | ||
const [counter, setCounter] = useState(0); | ||
|
||
const updateCounter = useCallback((amount: number) => { | ||
setCounter((prevCounter) => prevCounter + amount); | ||
}, []); | ||
|
||
const value = useMemo( | ||
() => ({ | ||
counter, | ||
updateCounter, | ||
}), | ||
[counter], | ||
); | ||
|
||
return <SampleContext.Provider value={value}>{children}</SampleContext.Provider> | ||
|
||
}; | ||
|
||
export function useSampleContext(): SampleContexType { | ||
const context = useContext(SampleContext); | ||
|
||
if (!context) { | ||
throw new Error( | ||
'useSampleContext must be used within a SampleContextProvider', | ||
); | ||
} | ||
|
||
return context; | ||
}; | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { createSelector } from 'reselect'; | ||
import { SampleGlobalState } from '../ducks/sample/sample'; | ||
|
||
type State = { | ||
sample: SampleGlobalState; | ||
}; | ||
|
||
export function selectSample(state: State): SampleGlobalState { | ||
return state.sample; | ||
} | ||
|
||
export const selectSampleCounter = createSelector( | ||
selectSample, | ||
(sample) => sample.counter, | ||
); |