Storing class instances in redux store
reactjs, redux
Solution
The fact that it's a singleton suggests to me that it's mutable, and that your reducers are modifying it directly instead of projecting new instances of it in response to actions. This is a misuse of redux and will cause react-redux to not recognize changes.
Problem
I have a few classes that I'd like to store instances of it in the redux store. The classes are singletons, and there is only one instance for each class. And I store them in the state with the name of the class (or something unique) as a key. I can store them just fine, but component's are not rerendering. I think it's because I'm always mutating the instance (because it's singleton) How should I store/use class instance in redux? What if it's a singletone instance? - edit I have plain objects/arrays in the singletone instance. Ah there's a function (action creator) in it as well. (which will be executed by redux-thunk) Actually, if that's problematic, or not a best practice, I guess I could store name of the actionCreator in the store and have name -> actionCreator map somewhere outside of the store. One of the action creator is below ``` function datetimeStockExtrasForDateRange(date) { return function (dispatch, getState) { const state = getState() const { calendar } = state const calendarPeriod = calendar.calendarPeriod let visibleDates = calendarUtil.visibleDates(date, calendarPeriod) const filteredDates = visibleDates.filter( (date) => { var dateFormatted = moment(date).format('YYYY-MM-DD') if (state.dates[dateFormatted] && state.dates[dateFormatted].available_datetime_stock_extras_fetched) { return false } return true }) if (!_.isEmpty(filteredDates)) { const { datetime_stock_rule_set_id } = state.calendar const date_start = filteredDates[0] const date_end = filteredDates[filteredDates.length - 1] dispatch( DatetimeStockExtra.fetchAvailableDatetimeStockExtrasForDateRangeAction( datetime_stock_rule_set_id, date_start, date_end ) ) } } } ```