question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Rendering a separate component at a later stage

See original GitHub issue

I’m having an issue rendering a separate component (with it’s own state) at a later time.

In this example, app2 is something I’d like to render at a later stage. Perhaps a user clicked a button and this component would populate a modal window. To make things as simple as possible, this example attempts to load app2 after 3 seconds via setTimeout().

no-worky

const choo = require('choo')

// app 1
const app1 = choo()

app1.model({
  state: {
    data: 'app1',
  },
  reducers: {
    update: (action, state) => ({data: action.value})
  }
})

function app1main (params, state, send) {
  return choo.view`<button onclick=${(evt) => send('update', {value: 'app2main'})}>${state.data}</button>`
}

app1.router((route) => [
  route('/', app1main)
])

const tree1 = app1.start({name: 'app1'})
document.body.appendChild(tree1)


// app 2
// subcribes to 'init' to trigger the start of a countdown
const app2 = choo()

function init (send) {
  send('start', { payload: send })
}

app2.model({
  state: {
    data: 'app2',
    count: 5,
    timer: null
  },
  reducers: {
    update: (action, state) => ({data: action.value}),
    start: (action, state)=> {{
      timer: setInterval(() => { action.payload('tick')}, 1000)
    }},
    tick: (action, state) => ({
      count: state.count - 1
    })
  },
  subscriptions: [init]
})

function app2main (params, state, send) {
  return choo.view`<button onclick=${(evt) => send('update', {value: 'app1main'})}>${state.data} ${state.count}</button>`
}

app2.router((route) => [
  route('/', app2main)
])

setTimeout(function() {
  const tree2 = app2.start({name: 'app2'})
  document.body.appendChild(tree2)
}, 3000)

You’d notice that the 2 apps render, but nothing is really happening. Removing the setTimeout from the rendering of app2 so they render at the same time, you’ll notice app2 will start decrementing.

worky

Is there a way around this ?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
toddselfcommented, Jun 30, 2016

There is no newb here 😄! I get stuck on the smallest things sometimes for hours.

Good luck! Hope you enjoy conducting the train!

0reactions
chrisbutterycommented, Jun 30, 2016

toot toot!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Rendering and Updating Data using Component Lifecycle ...
A React component can be created using a function or a class-based component, and the class-based component can use different lifecycle hooks.
Read more >
Delayed rendering of React components - Stack Overflow
I want to render the child components not at once but after some delay (uniform or different for each of the children). I...
Read more >
Render and Commit - React Docs
On initial render, React will call the root component. For subsequent renders, React will call the function component whose state update triggered the...
Read more >
When does React re-render components? - Felix Gerschau
When the state changes in the parent component (in this case, App ), the two Tile components will re-render, even though the second...
Read more >
How to understand a component's lifecycle methods in ReactJS
The name is self-explanatory. Mounting is the phase in which our React component mounts on the DOM (i.e., is created and inserted into...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found