Search code examples
ngxs

The Caveats of Sub States?


I am a newbie and hacking around with ngxs.

On the docs There are caveats to Sub States.

  • This is only intended to work with nested objects, so trying to create stores on nested array objects will not work.
  • Sub states can only be used once, reuse implies several restrictions that would eliminate some high value features. If you want to re-use them, just create a new state and inherit from it.

I believe I understand the first point to a small degree but I don't fully grasp what the second point means.

Would someone be able to expand on that?


Solution

  • it means that a single state class can't be a child of multiple parent classes. the workaround would be to create new states by extending. so

    @State({
      name: 'foo' // you can't have another state with this name
    })
    class MyState1 {}
    
    // so if you want to reuse the listeners and such from 'foo' you have to extend
    @State({
      name: 'bar'
    })
    class MyState2 extends MyState1 {}