Giter Club home page Giter Club logo

Comments (4)

markerikson avatar markerikson commented on August 17, 2024

Hmm. At the start of the function, the draft object should represent the latest state, so you should be able to just use that as a starting point for any comparisons.

I guess I'm not really seeing what the issue is here. Can you give a specific example, preferably with a CodeSandbox repro, of something you want to do that isn't working correctly?

from redux-toolkit.

simonsmith avatar simonsmith commented on August 17, 2024

Sure,

import { createReducer } from "redux-starter-kit";
export const initialState = {
  result: []
};

const Notifications = createReducer(initialState, {
  TEST(state, action) {
    console.log(state.result);
  }
});

Notifications(undefined, { type: "TEST" });

In the above example I'd expect the log to be [] but it is instead a Proxy object. I'm presuming this is because the draft object is passed as the 'state' and the actual state object is inaccessible. So I can write to the state, but I cannot read from it. Or have I misunderstood?

A use case for this might be wanting to compare a value on the existing state object before deciding what to update on the draft

Link - https://codesandbox.io/s/2v017oozy0 (needs browser console)

It looks like Immer supports passing an object as the third argument to pre-populate the draft. Perhaps that would solve the issue?

from redux-toolkit.

markerikson avatar markerikson commented on August 17, 2024

Immer is using a Proxy to wrap the state object, but as far as I know it should still let you access the underlying state as normal if you need to read values.

If I do console.log(state.result.length) in that sandbox, I see it print 3, as expected.

from redux-toolkit.

simonsmith avatar simonsmith commented on August 17, 2024

Ah, okay. I think that even though I'm not seeing [] I can still access things like .map() and .length.

Thanks for your time!

from redux-toolkit.

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.