Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 24 additions & 16 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,26 +1,34 @@
import React from "react";
import logo from "./logo.svg";
import "./App.scss";

import { useGlobalState } from "./state/useGlobalState";
import { UserActionTypes } from "./state/user/UserActions";
function App() {
const {state, dispatch} = useGlobalState();

const updateName = () => {
dispatch({
type: UserActionTypes.UpdateUserName,
name: "Emanuel"
})
}

const updateAge = () => {
dispatch({
type: UserActionTypes.UpdateUserAge,
age: 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;

11 changes: 8 additions & 3 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@ import ReactDOM from "react-dom";
import "./index.scss";
import App from "./App";
import * as serviceWorker from "./serviceWorker";
import { Provider } from "./state/Provider";



ReactDOM.render(
<React.StrictMode>
<App />
</React.StrictMode>,
<React.StrictMode>
<Provider>
<App />
</Provider>
</React.StrictMode>,
document.getElementById("root")
);

Expand Down
12 changes: 12 additions & 0 deletions src/state/Context.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import React, { createContext } from 'react';
import { UserActions } from './user/UserActions';
import { State, initialState } from './State';



const initialContext: { state: State; dispatch: React.Dispatch<UserActions> } = {
Comment thread
emanuelmustea marked this conversation as resolved.
Outdated
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

UserActions should not be there

state: initialState,
dispatch: () => {}
};

export const Context = createContext(initialContext);
10 changes: 10 additions & 0 deletions src/state/Provider.tsx
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>;
}
19 changes: 19 additions & 0 deletions src/state/Reducer.ts
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}) => {
Copy link
Copy Markdown
Collaborator Author

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.

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,
})
9 changes: 9 additions & 0 deletions src/state/State.tsx
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
}
5 changes: 5 additions & 0 deletions src/state/useGlobalState.tsx
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);
18 changes: 18 additions & 0 deletions src/state/user/UserActions.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export enum UserActionTypes {
UpdateUserName = "[User] Update User name",
UpdateUserAge = "[User] Update User age"

}

interface UpdateUserName {
type: UserActionTypes.UpdateUserName;
name: string;
}

interface UpdateUserAge {
type: UserActionTypes.UpdateUserAge;
age: number;
}


export type UserActions = UpdateUserName | UpdateUserAge;
20 changes: 20 additions & 0 deletions src/state/user/UserReducer.tsx
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;
}
};

10 changes: 10 additions & 0 deletions src/state/user/UserState.tsx
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"
}