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.

Side effects over-write state changes

See original GitHub issue

I came across an issue where I’m using AudioContext to create looping beats.

As a minimum example:

import { h, app } from 'hyperscript';

const initialState = {
  context: new AudioContext(),
  syncInterval: null,
  currentTime: 0,
  text: '',
};

const actions = {
  start: () => (state, actions) => {
    if (state.syncInterval) clearInterval(state.syncInterval);

    state.context.resume();

    return {
      syncInterval: setInterval(() => actions.setCurrentTime(state.context.currentTime), 10),
    }
  },

  stop: () => state => {
    if (state.syncInterval) clearInterval(state.syncInterval);
    state.context.suspend()
    return { syncInterval: null, currentTime: state.context.currentTime };
  },

  setCurrentTime: time => state => ({ currentTime: time }),

  setText: text => state => ({ text: text }),
};

const view = (state, actions) => (
  <div>
    {state.syncInterval === null && <button onclick={() => actions.start()}>Start</button>}
    {state.syncInterval !== null && <button onclick={() => actions.stop()}>Stop</button>}
    <div>Time: {state.currentTime}</div>
    <input value={state.text} onchange={(e) => actions.setText(e.target.value)} />
  </div>
);

const loop = app(initialState, actions, view, document.getElementById('app'));
loop.stop();

When you click the start button, try and type anything into the input box. The changes do no persist because setCurrentTime is merging with an old state.

Is this a bug or is there a better way to write this code?

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
SkaterDadcommented, Jan 27, 2018

Hi!

I believe the reason your code wasn’t working is because the <input onchange=...> only fires when the input is “blurred” (loses focus). Because of this, your keystrokes get thrown away since the value of the input is being set = the current state.text. To be honest, I’m confused why it works at all before you click the start button.

Changing your code to use <input oninput=...> works as expected, and fires on every keystroke. A good way to verify this behavior is to console.log() inside of the action.

Here is a Codepen showing that oninput works (converted to plain h functions instead of JSX): https://codepen.io/SkaterDad/pen/ddbObb

0reactions
jorgebucarancommented, Jan 28, 2018

I am glad you folks figured it out.

@mrozbarry If you feel something should be documented, please leave a comment in #531! 🙏

Read more comments on GitHub >

github_iconTop Results From Across the Web

Is there a way in React.js to run a side effect after `useState ...
Is there a way in React.js to run a side effect after `useState` when the value of the state doesn't change in functional...
Read more >
4 Working with side effects - React Hooks in Action
Recognizing types of side effects in components; Wrapping side effects ... When the state changes, React goes through the process again to update...
Read more >
Declarative side effects in React | by Matt Ellis | Medium
The optimal way to solve this problem is to elevate state — if you need new data to be fetched when id changes,...
Read more >
Side-effects in Compose - Android Developers
A side-effect is a change to the state of the app that happens outside the scope of a composable function. Due to composables'...
Read more >
Dancing between state and effects - a real-world use case
The problem is resetting that state - the effect would have to change state, causing a useless rerender, and it's not determistic to...
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