-
Notifications
You must be signed in to change notification settings - Fork 0
Create state management POC #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
emanuelmustea
wants to merge
4
commits into
master
Choose a base branch
from
add-state-management
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,26 +1,29 @@ | ||
| import React from "react"; | ||
| import logo from "./logo.svg"; | ||
| import "./App.scss"; | ||
| import { useGlobalState } from "./state/useGlobalState"; | ||
| import { UpdateUserName, UpdateUserAge } from "./state/user/UserActions"; | ||
|
|
||
| function App() { | ||
| const {state, dispatch} = useGlobalState(); | ||
|
|
||
| const updateName = () => { | ||
| dispatch(new UpdateUserName('Emanuel')) | ||
| } | ||
|
|
||
| const updateAge = () => { | ||
| dispatch(new UpdateUserAge(30)) | ||
| } | ||
|
|
||
|
|
||
| return ( | ||
| <div className="App"> | ||
| <header className="App-header"> | ||
| <img src={logo} className="App-logo" alt="logo" /> | ||
| <p> | ||
| Edit <code>src/App.tsx</code> and save to reload. | ||
| </p> | ||
| <a | ||
| className="App-link" | ||
| href="https://reactjs.org" | ||
| target="_blank" | ||
| rel="noopener noreferrer" | ||
| > | ||
| Learn React | ||
| </a> | ||
| </header> | ||
| <button onClick={updateName}>set name to Emanuel</button> | ||
| <button onClick={updateAge}>set age to 30</button><br /><br /> | ||
| Current name: <b>{state.user.name}</b><br /> | ||
| Current age: <b>{state.user.age}</b> | ||
| </div> | ||
| ); | ||
| } | ||
|
|
||
| export default App; | ||
|
|
This file contains hidden or 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 hidden or 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,3 @@ | ||
| export interface Action { | ||
| readonly type: string; | ||
| } |
This file contains hidden or 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,12 @@ | ||
| import React, { createContext } from 'react'; | ||
| import { State, initialState } from './State'; | ||
| import { Action } from './Action'; | ||
|
|
||
|
|
||
|
|
||
| const initialContext: { state: State; dispatch: React.Dispatch<Action> } = { | ||
| state: initialState, | ||
| dispatch: () => {} | ||
| }; | ||
|
|
||
| export const Context = createContext(initialContext); |
This file contains hidden or 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,10 @@ | ||
|
|
||
| import React, { useReducer } from 'react'; | ||
| import { Context } from './Context'; | ||
| import { initialState } from './State'; | ||
| import { reducer } from './Reducer'; | ||
|
|
||
| export function Provider({ children }: any) { | ||
| const [state, dispatch] = useReducer(reducer, initialState); | ||
| return <Context.Provider value={{ state, dispatch }}>{children}</Context.Provider>; | ||
| } |
This file contains hidden or 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,19 @@ | ||
| import { UserReducer } from "./user/UserReducer"; | ||
| import { State } from "./State"; | ||
|
|
||
| export const combineReducers = <T>(reducers: {[P in keyof T]: any}) => { | ||
| const reducerEntries = Object.entries<any>(reducers) as [keyof T, any][]; | ||
| return (state: T, action: any) => { | ||
| const nextState = {} as T; | ||
| for(const [key, reducer] of reducerEntries){ | ||
| const previousStateForKey = state[key]; | ||
| const nextStateForKey = reducer(previousStateForKey, action) | ||
| nextState[key] = nextStateForKey; | ||
| } | ||
| return nextState; | ||
| } | ||
| } | ||
|
|
||
| export const reducer = combineReducers<State>({ | ||
| user: UserReducer, | ||
| }) | ||
This file contains hidden or 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,9 @@ | ||
| import { UserState, initialUserState } from "./user/UserState"; | ||
|
|
||
| export interface State { | ||
| user: UserState | ||
| } | ||
|
|
||
| export const initialState: State = { | ||
| user: initialUserState | ||
| } |
This file contains hidden or 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,5 @@ | ||
| import { useContext } from 'react'; | ||
| import { Context } from './Context'; | ||
|
|
||
|
|
||
| export const useGlobalState = () => useContext(Context); |
This file contains hidden or 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,22 @@ | ||
| import { Action } from "../Action"; | ||
|
|
||
| export enum UserActionTypes { | ||
| UpdateUserName = "[User] Update User name", | ||
| UpdateUserAge = "[User] Update User age" | ||
|
|
||
| } | ||
|
|
||
|
|
||
|
|
||
| export class UpdateUserAge implements Action{ | ||
| public readonly type = UserActionTypes.UpdateUserAge; | ||
| constructor(public age: number) { } | ||
| } | ||
|
|
||
| export class UpdateUserName implements Action{ | ||
| public readonly type = UserActionTypes.UpdateUserName; | ||
| constructor(public name: string) { } | ||
| } | ||
|
|
||
|
|
||
| export type UserActions = UpdateUserName | UpdateUserAge; |
This file contains hidden or 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,20 @@ | ||
| import { UserActionTypes, UserActions } from "./UserActions"; | ||
| import { UserState } from "./UserState"; | ||
|
|
||
| export const UserReducer = (state: UserState, action: UserActions) => { | ||
| switch (action.type) { | ||
| case UserActionTypes.UpdateUserAge: | ||
| return { | ||
| ...state, | ||
| age: action.age | ||
| }; | ||
| case UserActionTypes.UpdateUserName: | ||
| return { | ||
| ...state, | ||
| name: action.name | ||
| }; | ||
| default: | ||
| return state; | ||
| } | ||
| }; | ||
|
|
This file contains hidden or 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,10 @@ | ||
| export type UserState = { | ||
| age: number; | ||
| name: string; | ||
| }; | ||
|
|
||
| export const initialUserState: UserState = { | ||
| age: 12, | ||
| name: "Initial name" | ||
| } | ||
|
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Simplified redux implementation of combineReducers.