Open cefn opened 1 year ago
Could retread the poorly architected project which is the core example for useReducer()
( https://react.dev/learn/extracting-state-logic-into-a-reducer )
Project has a codesandbox at https://codesandbox.io/s/cjpc7h?file=/App.js&utm_medium=sandpack
Could write an article prompted by this answer from Chat GPT...
Question: What's the best way to manage state in a React app?
Managing state in a React application is a crucial aspect of building scalable and maintainable applications. There are several ways to manage state in React, and the best approach often depends on the specific requirements of your application. Here are some common methods for managing state in a React app:
Choose the state management approach that best fits your application's needs. For small to medium-sized applications, local state with useState is often sufficient. As your application grows, you might find the need to incorporate other state management solutions like useReducer, Context API, or Redux.
Full answer was this and offered no ways which actually address the core challenge of React state (only updating UI for affected components) apart from Redux which is crap.
Managing state in a React application is a crucial aspect of building scalable and maintainable applications. There are several ways to manage state in React, and the best approach often depends on the specific requirements of your application. Here are some common methods for managing state in a React app:
1. **useState Hook:**
- The `useState` hook is a built-in React hook that allows functional components to manage local state. It's a simple and effective way to handle state within a component.
```jsx
import React, { useState } from 'react';
function ExampleComponent() {
const [count, setCount] = useState(0);
return (
<div>
<p>You clicked {count} times</p>
<button onClick={() => setCount(count + 1)}>
Click me
</button>
</div>
);
}
useReducer Hook:
useReducer
hook is useful when dealing with complex state logic that involves multiple sub-states or when state transitions are more intricate.import React, { useReducer } from 'react';
const initialState = { count: 0 };
function reducer(state, action) {
switch (action.type) {
case 'increment':
return { count: state.count + 1 };
default:
throw new Error();
}
}
function Counter() {
const [state, dispatch] = useReducer(reducer, initialState);
return (
<div>
<p>You clicked {state.count} times</p>
<button onClick={() => dispatch({ type: 'increment' })}>
Click me
</button>
</div>
);
}
Context API:
import React, { createContext, useContext, useState } from 'react';
const MyContext = createContext();
function MyProvider({ children }) {
const [myState, setMyState] = useState(initialState);
return (
<MyContext.Provider value={{ myState, setMyState }}>
{children}
</MyContext.Provider>
);
}
function useMyState() {
const context = useContext(MyContext);
if (!context) {
throw new Error('useMyState must be used within a MyProvider');
}
return context;
}
Redux:
// actions.js
export const increment = () => ({ type: 'INCREMENT' });
// reducers.js
const initialState = { count: 0 };
export const counterReducer = (state = initialState, action) => {
switch (action.type) {
case 'INCREMENT':
return { count: state.count + 1 };
default:
return state;
}
};
// store.js
import { createStore } from 'redux';
import { counterReducer } from './reducers';
const store = createStore(counterReducer);
// App.js
import React from 'react';
import { Provider } from 'react-redux';
import store from './store';
import Counter from './Counter';
function App() {
return (
<Provider store={store}>
<Counter />
</Provider>
);
}
Choose the state management approach that best fits your application's needs. For small to medium-sized applications, local state with useState
is often sufficient. As your application grows, you might find the need to incorporate other state management solutions like useReducer
, Context API, or Redux.
See reference examples from VanJS
Something which shows the improvements made by centralising state
Could upload a response video to https://github.com/WebDevSimplified/react-hangman (problem part of video at https://youtu.be/-ONUyenGnWw?t=1980 ) which shows the simplification and functionality of the same app but with a
@watchable/state
implementation.https://codesandbox.io/s/github/reduxjs/redux-essentials-counter-example/tree/master/?from-embed=&file=/src/features/counter/counterSlice.js