Skip to content

Commit ef90d93

Browse files
Troy's edits Merge pull request #1 from tmr08c/tmr08c-suggestions-for-react-context-post
Makes a few small edits to React Context Post
2 parents ae712ac + e998b1f commit ef90d93

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

content/articles/when-a-simple-react-context-gets-out-of-hand.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -75,16 +75,16 @@ Let's go ahead and solve that in the `TimelineContext`.
7575
```javascript
7676
const TimelineContextProvider = ({ children }) => {
7777
const [posts, setPosts] = useState([]);
78-
const [ selectedPost, setSelectedPost] useState(null)
79-
const [ selectedComment, setSelectedComment] useState(null)
78+
const [selectedPost, setSelectedPost] = useState(null)
79+
const [selectedComment, setSelectedComment] = useState(null)
8080

8181
const state = { posts, selectedPost, selectedComment };
8282
const actions = { setPosts, setSelectedPost, setSelectedComment }
8383

8484
return (
85-
<AuthContext.Provider value={{ state, actions}}>
85+
<TimelineContext.Provider value={{ state, actions}}>
8686
{children}
87-
</AuthContext.Provider>
87+
</TimelineContext.Provider>
8888
);
8989
};
9090
```
@@ -104,8 +104,8 @@ This is fairly simple. We can just throw in a `useEffect` and boom.
104104
```javascript
105105
const TimelineContextProvider = ({ children }) => {
106106
const [posts, setPosts] = useState([]);
107-
const [ selectedPost, setSelectedPost] useState(null)
108-
const [ selectedComment, setSelectedComment] useState(null)
107+
const [selectedPost, setSelectedPost] = useState(null)
108+
const [selectedComment, setSelectedComment] = useState(null)
109109

110110
const state = { posts, selectedPost, selectedComment };
111111
const actions = { setPosts, setSelectedPost, setSelectedComment }
@@ -126,7 +126,7 @@ This is fairly simple. We can just throw in a `useEffect` and boom.
126126

127127
Now let's say for testing purposes we want to add initial{SelectedPost and SelectedComment}. Stupid simple. Or is it?
128128

129-
The way we currently have it set up, the useEffect will set our `initialSelectedComment` to null on the first render. OOOO no a side useEffect!!!
129+
The way we currently have it set up, the `useEffect` will set our `initialSelectedComment` to `null` on the first render. OOOO no a side useEffect!!!
130130

131131
So our context then turns into:
132132

@@ -212,7 +212,7 @@ Not a huge win IMO, but now the team is happier.
212212

213213
After working with React hooks for a year, I've come to the conclusion that `useEffect` in a context is probably a bad idea. (I'd love to see examples where you've made this work BTW).
214214

215-
A more concrete rule that I've landed on is that we should not have a useEffect in our app that relies on global state. I kind of see this a sharp knife that could easily poke your eye out. It raises the barrier to work on a project for people that don't work in the frontend day in and day out. Even for someone working in the codebase, it's something they always have to keep in the back of their mind. "If I change {X}, this callback will run, and do I need to modify it?".
215+
A more concrete rule that I've landed on is that we should not have a `useEffect` in our app that relies on global state. I kind of see this a sharp knife that could easily poke your eye out. It raises the barrier to work on a project for people that don't work in the frontend day in and day out. Even for someone working in the codebase, it's something they always have to keep in the back of their mind. "If I change {X}, this callback will run, and do I need to modify it?".
216216

217217
My solution to this is to always (well prolly 95% of the time) use `useReducer` in global state and to never have a `useEffect` depend on a piece of global state.
218218

@@ -229,7 +229,7 @@ const initialState = {
229229
};
230230
```
231231

232-
Well, that was easy enough! Defining our initial state lets us see all of our global state at a glance. Any time we want to add something to our global state, we can start by adding a sensible default to our initialState object. For example, `isLoggedIn` is initially false, and `posts` is initially an empty array.
232+
Well, that was easy enough! Defining our initial state lets us see all of our global state at a glance. Any time we want to add something to our global state, we can start by adding a sensible default to our `initialState` object. For example, `isLoggedIn` is initially false, and `posts` is initially an empty array.
233233

234234
## Reducery, my dear Watson
235235

@@ -331,11 +331,11 @@ const AppProvider = ({ initialState, reducer, children }) => {
331331
};
332332
```
333333

334-
Well, that's a lot cleaner. My team works in a [Rails](https://rubyonrails.org/) monolith which is why I've decided to have `initialState` and the `reducer` to be props for the AppProvider. This approach allows us to use the same provider for any React app that we decide to create.
334+
Well, that's a lot cleaner. My team works in a [Rails](https://rubyonrails.org/) monolith which is why I've decided to have `initialState` and the `reducer` be props for the `AppProvider`. This approach allows us to use the same provider for any React app that we decide to create.
335335

336336
# Conclusion
337337

338-
Currently, this is my favorite way to [with some extra magic I'll blog about later] to manage global state in a React app.
338+
Currently, this is my favorite way to [with some extra magic I'll blog about later] manage global state in a React app.
339339
- No added dependencies.
340340
- No side effects on global state that have to be memorized.
341341
- Each interation is mapped to a single encapsulated action.

0 commit comments

Comments
 (0)