jorgebucaran / hyperapp

1kB-ish JavaScript framework for building hypertext applications
MIT License
19.08k stars 780 forks source link

Should Slices support Array type? #472

Closed sam-chuang closed 6 years ago

sam-chuang commented 6 years ago

From the Slices doc, it doesn't mentioned supported type, so I tried to use Array with it, then I got error, refer to test code here

From the merge function in src, it seems that Slices only support object type.

SkaterDad commented 6 years ago

@sam-chuang You are correct, only objects are supported. This is because state slices are intended to reduce boilerplate when updating deeply nested object keys. It's a similar concept to Redux/Vuex modules/namespacing.

If you want your state to have an array, the action modifying it needs to be at the same level in the actions tree, but with a different name.

import { app, h } from 'hyperapp'

const appActions = app({
  state: {
    arr: [1, 2, 3]
  },
  actions: {
    addToArr: (num) => (state) => ({ arr: [...state.arr, num] })
  },
  view: (state) => h('pre', null, JSON.stringify(state, null, 2))
})

appActions.addToArr(4)
jorgebucaran commented 6 years ago

@sam-chuang Yep, slices only support object types, which is to say, your state can only be an object.

Now, way back the state could be anything, a number, string, array or object, but then we changed it to only objects.

But this was before slices were introduced, and now I wonder if it would be worth having another look at state that could be any type.

dima-takoy-zz commented 6 years ago

How it looks with model entry?

jorgebucaran commented 6 years ago

@dmitry-vakhnenko Good question, if modules are passed, then this loses all its value, although, even if that doesn't go through, I don't think I'd want to go back to any-type-state.