keajs / kea

Batteries Included State Management for React
https://keajs.org/
MIT License
1.94k stars 51 forks source link

pass component props to logic #133

Closed SeifESlimene closed 2 years ago

SeifESlimene commented 3 years ago

Hello,

Is there any possibility to pass a component prop to logic to use them as the default value for a reducer for example?

For Example, I have a component B which has a parent component A, A pass to B a prop called foo, I want that foo to become the default value for a reducer called values

export default kea({ reducers: ({ actions }) => ({ values: [Here I want to use the prop foo passed from component A to component B, PropTypes.object, {}], }) })

mariusandra commented 3 years ago

Sure, just do:

export default kea({
  reducers: ({ actions, props }) => ({
    values: [props.foo, PropTypes.object, {}],
  })
})
SeifESlimene commented 3 years ago

Hello again,

Thank you but It doesn't work with me! I made a little example and it works with a default value of '1' for the text reducer and when I change it to props. name it gives me an error.

You can check the sandbox here: https://codesandbox.io/s/kea-three-buttons-forked-0t5xg

My goal is to not use 'useEffect' anymore and 100% rely on kea to do the job instead as it's a nice and great refactoring opportunity for me.

mariusandra commented 3 years ago

Hey @SeifESlimene , sorry but I don't really see what you mean with that codesandbox. Is it a complete example? I'd gladly help if I could see what's broken. Right now I don't see any props usage at all.