reflux / refluxjs

A simple library for uni-directional dataflow application architecture with React extensions inspired by Flux
BSD 3-Clause "New" or "Revised" License
5.36k stars 330 forks source link

Extending Reflux Store #539

Closed pranavrajs closed 6 years ago

pranavrajs commented 6 years ago

I have a situation where I have shared states and, actions which differs according to different view.

I am thinking of a SuperClass which extends RefluxStore and subclasses which extends Superclass.

For eg:

class Store1 extends Reflux.store {}

class Store2 extends Store1  {}

class Store3 extends Store1 {}

Is this correct ?

BryanGrezeszak commented 6 years ago

Normally I'd say this sort of thing is for stack overflow, but since this is the first time extending Reflux.Store multiple levels deep I'll take the chance to comment:

Basically yes (though remember it's Reflux.Store, capital "S").

The most noticeable difference will be in the constructor, you don't want to declare your starting state as this.state = {foo:'bar'}, because it'll completely replace any state made from the Store it inherited from (that's not really a Reflux specific thing, just a fact of inheritance of properties in general). You'd do more like this.state.foo = 'bar';, since this.state was already created in the super class, and you're just adding props to it.

jsfiddle example inheriting 3 levels deep: https://jsfiddle.net/92zz7o3m/22/