Code Monkey home page Code Monkey logo

Comments (6)

JM-Mendez avatar JM-Mendez commented on July 25, 2024 4

@xomyaq sure. You can type your maps at any level like this.

type firstReducer = () => {}
type secondReducer = () => {}

interface AllReducers {
  root: firstReducer,
  ui: secondReducer,
}

const initialState: Map<keyof AllReducers, AllReducers[keyof AllReducers]> = Map()

from redux-immutable.

JM-Mendez avatar JM-Mendez commented on July 25, 2024 3

You can use records in your redux app. The immutable api set method on Record collections works just fine. But the OP wanted to use records in order to use dot notation, and that only works for accessing property values, not setting them. Which means he'd still be forced to use the immutable api.

myRecord.b // 3
myRecord.b = 5 // throws Error

Also, you have to explicitly create each record instance with every single property value when initializing your state.

recordState: Record({ a: 1, b: 2 })

This works fine if you know for a fact that your initial state will always be initialized to a:1, b:2, and will never add another property. So this won't work:

recordState.set('c', '3') // throws no error since immutable ignores setting non-existent record properties

Thus you'd be forced to mix a top level record state with maps, and then will end up accessing them using the immutable api anyways.

Lastly, v4.0.0-rc.1 introduced breaking changes to records. They are no longer an immutable collection type, so sequence api methods like map, filter, forEach are no longer available.
https://github.com/facebook/immutable-js/releases/tag/v4.0.0-rc.1


Maps can be typed just like records. All that having records buys you is faster access and a guarantee of having specific properties always initialized. Personally, I use a map as the top level state, and then use records for my collections and all works perfectly. And I'm able to type them using typescript.

from redux-immutable.

JM-Mendez avatar JM-Mendez commented on July 25, 2024

Immutable records cannot be instantiated generically like map can. Have you tried to create a record, and then instantiate the initial state like so? I set the default values to undefined, but they can be anything you'd like.

const expectedState = Record({
    bar: undefined,
    foo: undefined
})

const initialState = expectedState({
    bar: barReducer,
    foo: fooReducer
})

// if the defaults are the same
const expectedState = Record({
    bar: barReducer,
    foo: fooReducer
})

const initialState = expectedState()

from redux-immutable.

JustFly1984 avatar JustFly1984 commented on July 25, 2024

from redux-immutable.

sylvain-pauly avatar sylvain-pauly commented on July 25, 2024

@JustFly1984 Can you please, explain what does he mean by

Records don't allow the set method after creation

https://facebook.github.io/immutable-js/docs/#/Record/set

Am I missing something ? I started to make records for my reducers's states, mostly to be able to type them with flow (since the Maps shape can't be typed).

Please, tell me it's ok with Immutable v4... I'm starting to think that Immutable is not a library you want in your redux app - bad news for me, I have a huge app running on Immutable Redux state.

from redux-immutable.

i11v avatar i11v commented on July 25, 2024

@JM-Mendez, hi! Sorry for unrelated question: can you please provide an example of typing Map on top level state? Thank you :)

from redux-immutable.

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.