Skip to content

[RFC] Derived state in model #159

@kyleslight

Description

@kyleslight

Sometimes we have one state and its value will be changed if and only if another state is changed. Nowadays we have to update their states manually in actions, for instance:

{
  state: {
    num: 3,
    sqauredNum: 9
  },
  actions: {
    setNum: num => {
       return state => {
           state.num = num
           state.sqauredNum = num * num
       }
    }
  }
}

Is there a way to manage only core states and make the derived states (like sqauredNum) be re-calculated automatically (like Selectors in https://recoiljs.org/docs/introduction/core-concepts)

const defaultFunc = () => {}

{
  state: {
     num: 3,
     sqauredNum: ({ num }) => num * num // calculated state
     func: defaultFunc // function handler
  }
}

...

const App = () => {
  const [{ sqauredNum }] = useStore('xxx')
  // use sqauredNum
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions