Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
STATUS: useContext structured and linked components achieved | Typesc…
…ript: 1 major error
- Loading branch information
Showing
10 changed files
with
214 additions
and
49 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
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
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,5 @@ | ||
import React from 'react'; | ||
|
||
const AppContext = React.createContext({}); | ||
|
||
export default AppContext; |
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,45 @@ | ||
import { useState } from 'react'; | ||
import initialState from '../initialState'; | ||
|
||
interface StateObject { | ||
id: string; | ||
image: string; | ||
title: string; | ||
price: number; | ||
description: string; | ||
} | ||
interface AppState { | ||
cart?: StateObject[]; | ||
products?: StateObject[]; | ||
} | ||
|
||
interface InitState { | ||
addToCart: (payload: StateObject) => void; | ||
removeFromCart: (payload: StateObject, indexToRemove: number) => void; | ||
state: AppState; | ||
} | ||
|
||
const useInitialState = (): InitState => { | ||
const [state, setState] = useState(initialState); | ||
|
||
const addToCart = (payload: StateObject) => { | ||
setState({ | ||
...state, | ||
cart: [...state.cart, payload], | ||
}); | ||
}; | ||
const removeFromCart = (_payload: StateObject, indexToRemove: number) => { | ||
setState({ | ||
...state, | ||
cart: state?.cart?.filter((_item, indexCurrent) => indexCurrent !== indexToRemove), | ||
}); | ||
}; | ||
|
||
return { | ||
addToCart, | ||
removeFromCart, | ||
state, | ||
}; | ||
}; | ||
|
||
export default useInitialState; |
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