cefn / watchable

Repo for @watchable/store and supporting packages.
MIT License
5 stars 1 forks source link

Author reference examples #2

Open cefn opened 1 year ago

cefn commented 1 year ago

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

cefn commented 10 months 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

cefn commented 10 months ago

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>
     );
   }
  1. useReducer Hook:

    • The 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>
     );
    }
  2. Context API:

    • React's Context API allows you to share state between components without explicitly passing props through every level of the component tree. It is useful for global or shared state management.
    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;
    }
  3. Redux:

    • For larger applications with complex state management needs, Redux is a predictable state container that can help manage the global state of your application.
    // 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.

cefn commented 8 months ago

See reference examples from VanJS

https://vanjs.org/demo#game