| I think there’s a strong argument to be made here that the actual code in this scenario is flawed. What Svelte is doing is simply covering up the flaws in your code by short circuiting an infinite loop scenario which has been coded into your app. This will have unexpected side effects, however. That being said, I think useEffects is far too overloaded in React, and is currently used for completely orthogonal purposes. At he very least it’s used for the following completely unrelated purposes. 1. Component setup and graceful dismantling.
2. Syncing with external state.
3. Running code specific to a certain prop being changed. I think the React team would be highly justified if they created a separate hook for #3 at least. Maybe a usePropsChanged hook that is called when one of the props change, and provides an isChanged function that you can pass a prop to, to check if it’s actually changed (I’m sure someone can come up with a better design than I have in the process of writing this comment). That would be a semantic which would actually allow you to avoid the code flaw that your code above includes, as opposed to brute force papering it over like Svelte appears to do. |