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

Unmount a component #101

Closed
chrisbuttery opened this issue Jun 30, 2016 · 1 comment
Closed

Unmount a component #101

chrisbuttery opened this issue Jun 30, 2016 · 1 comment

Comments

@chrisbuttery
Copy link
Contributor

I'm wondering how best to unmount a component.
This sort of ties in with this issue #98, where a setInterval effect is calling a reducer every second.

What would be the best approach to destroy the setInterval and the component ?

requirebin

const choo = require('choo')
const app = choo()

function init (send) {
  send('start')
}

function decrement (state) {
  if (state.count === 0) {
    // implode
  } else {
    return {count: state.count - 1}
  }
}

app.model({
  state: {
    data: 'app',
    count: 5
  },
  effects: {
    start: (action, state, send) => setInterval(() => send('tick'), 1000)
  },
  reducers: {
    tick: (action, state) => decrement(state)
  },
  subscriptions: [init]
})

function main (params, state, send) {
  return choo.view`<button>${state.count}</button>`
}

app.router((route) => [
  route('/', main)
])

setTimeout(function() {
  const tree = app.start({name: 'app'})
  document.body.appendChild(tree)
}, 3000)
@yoshuawuyts
Copy link
Member

yoshuawuyts commented Jun 30, 2016

Use https://github.com/shama/on-load; check out #1 for current state of landing this natively in choo (it is going to land; can't exactly tell when tho).

Closing as I think this should get you there. Keep an eye on the issues in on-load and bel if you run into trouble; feel free to ask back here if there's anything else tho. Cheers! ✨

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