Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Advice needed] on triggering re-render of components when state changes #6

Closed
xzilja opened this issue Jan 31, 2017 · 2 comments
Closed

Comments

@xzilja
Copy link

xzilja commented Jan 31, 2017

I'm loving emitter based state management via smitty so far. My biggest roadblock at the moment is that I can't simply do something like

<Component amount={amount.state.value} />

and expect Component to update when value changes, like we could via redux props. At the moment I need to use state and add a lot of amount.on(SOMETHING) listeners that then call functions to update that state hence re-rendering component with new values. Furthermore I need to remember to always do amount.off during un-mounting.

This brings me to reason behind this question. Can you advise what could be done here in order to achieve this i.e. something like mapStateToProps but using smitty?

@xzilja xzilja changed the title Advice on triggering re-render of components when state changes [Advice needed] on triggering re-render of components when state changes Jan 31, 2017
@tkh44
Copy link
Owner

tkh44 commented Feb 1, 2017

Yea, check out https://github.com/tkh44/react-smitty or https://github.com/tkh44/preact-smitty

Here is an example.
http://codepen.io/tkh44/pen/qRjjwb

const mapStateToProps = (state) => ({ ui: state.get('ui') })
const Demo = connect(mapStateToProps)(({ ui, emit }) => {
  return h(
    'div',
    {
      className: 'demo'
    },
    h(Header, { emit }),
    h(Body, { ui, emit })
  )
})

It is listed in the docs but I should probably make it more prominent.

@xzilja
Copy link
Author

xzilja commented Feb 1, 2017

Ah that does the trick, missed it in documentation. Thank you :)

@xzilja xzilja closed this as completed Feb 1, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants