Side effects over-write state changes
See original GitHub issueI 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:
- Created 6 years ago
- Comments:8 (8 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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 thevalue
of the input is being set = the currentstate.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 toconsole.log()
inside of the action.Here is a Codepen showing that
oninput
works (converted to plainh
functions instead of JSX): https://codepen.io/SkaterDad/pen/ddbObbI am glad you folks figured it out.
@mrozbarry If you feel something should be documented, please leave a comment in #531! 🙏