Skip to content
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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Components keep being added when updating state #4274

Closed
1 task done
yy0931 opened this issue Feb 5, 2024 · 1 comment · Fixed by #4281
Closed
1 task done

Components keep being added when updating state #4274

yy0931 opened this issue Feb 5, 2024 · 1 comment · Fixed by #4281
Labels

Comments

@yy0931
Copy link

yy0931 commented Feb 5, 2024

  • Check if updating to the latest Preact version resolves the issue

I'm having a problem where a component is repeatedly added to document.body, even though it's only used once in the component. Below is a simplified version of the code, where <B /> keeps getting added repeatedly. This issue doesn't happen with React. Am I missing something?

import { render } from "preact"
import { useEffect, useState } from "preact/hooks"

const B = () => <div>B</div>

const Test = () => {
    const [state, setState] = useState(true)
    useEffect(() => {
        const timer = setInterval(() => {
            setState((s) => !s)
        }, 250)
        return () => { clearInterval(timer) }
    }, [])

    if (state) {
        return <div>
            <B />
            <div></div>
        </div>
    } else {
        const cond = false
        return <div>
            <div></div>
            {cond && <div></div>}
            <B />
            <div></div>
        </div>
    }
}

render(<Test />, document.body)

To Reproduce, you can copy-paste the above code to https://preactjs.com/repl to see that the text "B" is continuously added.

Expected behavior
Only a single instance of <B /> should be shown.

Edit: I've further simplified the code.

@yy0931 yy0931 changed the title Eleements keep added when updating state Components keep being added when updating state Feb 5, 2024
@marvinhagemeister
Copy link
Member

marvinhagemeister commented Feb 5, 2024

With a slightly modified variant I'm able to finally reproduce #4194 .

import { render } from "preact"
import { useEffect, useState } from "preact/hooks"

const B = () => <div>B</div>

const Test = () => {
    const [state, setState] = useState(true)
    useEffect(() => {
        const timer = setInterval(() => {
            setState((s) => !s)
        }, 250)
        return () => { clearInterval(timer) }
    }, [])

    if (state) {
        return <div>
            <B />
            <div></div>
        </div>
    } else {
        const cond = false
        return <div>
            <div></div>
            {null}
            <B />
        </div>
    }
}

render(<Test />, document.body)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants