Coder Social home page Coder Social logo

Comments (5)

gunar avatar gunar commented on June 26, 2024 3

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}
    />);
}

from react-dom-confetti.

RXminuS avatar RXminuS commented on June 26, 2024 2

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} />);
}

from react-dom-confetti.

AdiBev avatar AdiBev commented on June 26, 2024

Is there way to do this without hooks?

from react-dom-confetti.

mjoyce91 avatar mjoyce91 commented on June 26, 2024

@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.

from react-dom-confetti.

MartinDevillers avatar MartinDevillers commented on June 26, 2024

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.

from react-dom-confetti.

Related Issues (18)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.