-
-
Notifications
You must be signed in to change notification settings - Fork 349
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鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Test effects #80
Test effects #80
Conversation
I think this is another bug, because I just fixed #79 |
Hey, I don't quite understand the example of this case. Maybe like this? function App () {
let effects = []
let effectNum = 1
const [state, setState] = useState(true)
useEffect(() => {
const currentEffectNum = effectNum++
effects.push(`effect ${currentEffectNum}`)
console.log(effects) // remove : ["effect 1", "cleanUp 1", "effect 2"]
return () => {
effects.push(`cleanUp ${currentEffectNum}`)
}
})
return (
<div>
{state && <div>111</div>}
<button onClick={() => setState(false)}>remove</button>
</div>
)
} |
From the React guide, "effects with cleanup":
With So the test runs in 3 steps to demonstrate this behavior. Step 1: {
content: <Component/>,
test: () => {
expect(effects).toEqual(["effect 1"])
}
}, Step 2: {
content: <Component/>,
test: () => {
expect(effects).toEqual(["cleanUp 1", "effect 2"])
}
}, Step 3: {
content: <div>removed</div>,
test: () => {
expect(effects).toEqual(["cleanUp 2"])
}
} |
Can you give me a use case? The use case I try here is the same as the result of react, so I think the implementation of fre should be correct now |
Here's a demo that prints the effects and clean-ups for each operation: https://codesandbox.io/s/fre-demo-fgymz You can switch it between imports from If I repeat an exact sequence of clicks with That's As you can see, You need to revert this change - the test should be correct, and should pass if your output is like the one on the left. |
Thank you very much! I will debug and fix it馃檹馃檹馃檹 |
I fixed this bug. Now it looks the same as react, but I still can't run through the test case. Can you find the reason? |
Looks like you still need to roll back the change you made to the test? |
The test is still not necessarily correct. Can you change the example for differentiation? I fixed the last example. |
I am very concerned about this bug. Our purpose is to find out the difference between react and fre, not the test is right or not. |
But I don't see any new commits to anything other than files in the Maybe you didn't push all your changes? (or are they on a branch?) I just fetched Here they are aligned, side-by-side, for easier comparison: Red lines show effects getting fired twice. Everything else looks correct, just that sometimes the same effects and clean-up get triggered twice? |
I commit again, but from here, |
It looks like effects are triggered twice only during updates involving the |
It seems like this. We could keep up with react as much as possible. |
The above is a good summary, I think |
Thanks, but it's just a toy for now - a lot more work is required to support all of the life-cycle methods, etc... I think it's possible, though I'm not 100% sure yet. What's interesting, is this would be the opposite of what Preact does: they have the Component API built into the core, and hooks are an optional add-on - Fre would instead have hooks built into the core, with class-based components as an optional add-on. 馃憖 Another really interesting thing about this, is that, with React/Preact, you have to choose either class-based components or hooks... But with this approach, Fre would actually allow mixing class-based components and hooks - you'd be able to call hooks from the |
Ah~as you said, This is really amazing. |
Alright, here's a failing test that should demonstrate the problem we discussed in #79 .
As you can see, we're good on
useEffect(f, [x])
as well asuseEffect(f, [])
. 馃憤The problem is with
useEffect(f)
which seems to trigger some effects and cleanups twice -As you will see when running the test:
So, at removal,
cleanUp 1
andeffect 2
are currently both being run a second time!Note that I did already merge your last commit which didn't fix it.
Hopefully this will help you fix it 馃槉