Code Monkey home page Code Monkey logo

Comments (4)

mweststrate avatar mweststrate commented on July 26, 2024 1

@ianstormtaylor I think I get what you are getting at. I think the first solution is nicer, the second one looks incorrect, as immer wouldn't receive the new user after an update.

However, I think this is a simpler solution that doesn't need new api's:

import {produce} from immer

function Example() {
  const [user, _setUser] = useLocalStorage('user')
  const updateUser = useMemo(() => produce(_setUser), [])
  
  updateUser(user => {
    user.name = 'New Name'
  })
}

from use-immer.

ianstormtaylor avatar ianstormtaylor commented on July 26, 2024

An alternative way to handle this nicely would be to allow useImmer to take a second argument that is a callback called whenever the update happens. Which would allow you to write the same example as:

function Example() {
  const [user, setUser] = useLocalStorage('user')
  const [, updateUser] = useImmer(user, setUser)
  
  updateUser(user => {
    user.name = 'New Name'
  })
}

I think this is actually more flexible, so this would be the better solution.

from use-immer.

akshayjai1 avatar akshayjai1 commented on July 26, 2024

Hi Ian,

Can you please update the Example component, I am not able to figure out what this component intends to do, and that will also help get more clarity about your issue.

Thank you

from use-immer.

Newbie012 avatar Newbie012 commented on July 26, 2024

@mweststrate How would you implement such a thing using reducer?

Edit - I have managed to do so with useEffect:

export function useCart() {
  const [storageCart, setStorageCart] = useLocalStorage("cart", initalCart);
  const [cart, dispatch] = useImmerReducer<Cart>(reducer, storageCart);

  React.useEffect(() => {
    setStorageCart(cart);
  }, [cart, setStorageCart]);

  return [cart, dispatch] as const;
}

from use-immer.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.