-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'Feature-Load-Item-Headers-Only' of github.itap.purdue.e…
…du:ECN/webqueue2 into Feature-Load-Item-Headers-Only
- Loading branch information
Showing
5 changed files
with
81 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import React, { useState, createContext, useContext, useEffect } from "react"; | ||
|
||
const ItemContext = createContext(); | ||
const ItemSetterContext = createContext(); | ||
|
||
export const useItem = () => useContext(ItemContext); | ||
export const useItemSetter = () => useContext(ItemSetterContext); | ||
|
||
|
||
|
||
export default function ItemProvider({ children }) { | ||
const [item, setItem] = useState( {} ); | ||
|
||
useEffect( _ => console.log("Current Item is: ", item)); | ||
|
||
const setItemWrapper = (newItem) => { | ||
console.log("Old Item: ", item); | ||
setItem(newItem); | ||
console.log("Set new item."); | ||
} | ||
|
||
return ( | ||
<ItemContext.Provider value={item}> | ||
<ItemSetterContext.Provider value={setItemWrapper}> | ||
{children} | ||
</ItemSetterContext.Provider> | ||
</ItemContext.Provider> | ||
); | ||
}; |
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,41 @@ | ||
Utility component that uses [React Contexts](https://reactjs.org/docs/context.html) [React Stateful Variables](https://reactjs.org/docs/hooks-state.html) to provide global access to the active item object. | ||
|
||
Two functions are exported: | ||
|
||
Function | Descrioption | ||
- | - | ||
`useItem` | Returns a reference to the state variable holding the current Item. Defaults to `false`. | ||
`useItemSetter` | Returns a reference to the state variable update function. | ||
|
||
For an in depth explanation of this pattern, see [this GitHub comment](https://github.itap.purdue.edu/ECN/webqueue2/issues/15#issuecomment-341). | ||
|
||
|
||
## Usage | ||
```jsx static | ||
// App | ||
<ItemProvider> | ||
<SomeComponent /> | ||
</ItemProvider> | ||
``` | ||
|
||
```jsx static | ||
// SomeComponent | ||
import { useEffect } from "react"; | ||
import { useItem, useItemSetter } from "ItemProvider"; | ||
|
||
const activeItem = useItem(); | ||
const setActiveItem = useItemSetter(); | ||
|
||
useEffect( | ||
let item = someFuncToGetItem(); | ||
setActiveItem(item); | ||
); | ||
|
||
return( | ||
{ | ||
item | ||
? <p>{`${activeItem.queue} ${activeItem.number} was last updated ${activeItem.lastUpdated}.`}</p> | ||
: <p>No item is currently loaded.</p> | ||
} | ||
); | ||
``` |
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 @@ | ||
export {default, useItem, useItemSetter } from "./ItemProvider"; |
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