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

Confetti on initial load #16

Open
VisheshSingh opened this issue Mar 29, 2019 · 5 comments
Open

Confetti on initial load #16

VisheshSingh opened this issue Mar 29, 2019 · 5 comments

Comments

@VisheshSingh
Copy link

Hey,

I was able to get it to work upon a state change. Wondering if there is a way to make the confetti appear when a component loads initially without any state change. I tried setting the active attribute to true but that didn't work. Help will be appreciated!

@RXminuS
Copy link

RXminuS commented Jun 11, 2019

Hooks to the rescue :-)

const Yay : React.FC<{}> = () => {
  const trigger = useBoolean(false);
  useEffect(() => {
    setTimeout(trigger.setTrue, 300);
    //eslint-disable-next-line
  }, [])
  return (<Confetti active={trigger.value} config={confettiConfig} />);
}

@AdiBev
Copy link

AdiBev commented Nov 25, 2019

Is there way to do this without hooks?

@mjoyce91
Copy link

mjoyce91 commented Jan 13, 2020

@AdiBev you could put it inside componentDidMount. Untested example:

this.state = {
  active: false
}
componentDidMount() {
  setTimeout(() => {
    this.setState({ active: true });
  }, 300);
}
...

You probably don't even need the timeout if you use it within a componentDidMount.

@gunar
Copy link

gunar commented May 27, 2020

Alternatively, we can drop the setTimeout and use a callback ref to trigger the animation as soon as the component renders:

const Component = () => {
  const [active, setActive] = useState(false);
  return (
    <Confetti
      ref={() => { setActive(true) }}
      active={active}
      config={confettiConfig}
    />);
}

@MartinDevillers
Copy link

This is the first thing I ran in as well. I kind of expected <Confetti active /> to start throwing me some confetti ;-) I fixed it by wiring the active prop to some hook that changes pretty early in my components lifecycle. The document is clear though: the effect is triggered when the active prop changes from falsy to truthy. It's the change that triggers it; not the actual value.

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

No branches or pull requests

6 participants