I keep writing this useMount() hook. It's easier to scan for in code than useEffect. Basically my brain can't remember this: useEffect(fn) // run after each render useEffect(fn, []) // runs after mountpic.twitter.com/dxdtUElVET
U tweetove putem weba ili aplikacija drugih proizvođača možete dodati podatke o lokaciji, kao što su grad ili točna lokacija. Povijest lokacija tweetova uvijek možete izbrisati. Saznajte više
Side effects make this slightly less clear-cut. useEffect(() => { const obs = new IntersectionObserver((entries) => { ... }) obs.observe(blah) return () => obs.unobserve(blah) }, [])
My closest attempt to make this fit into the box of useEffect is to move the initialization into useMemo: const obs = useMemo(() => new IntersectionObserver((entries) => { ... }) ) useEffect(() => { obs.observe(blah) return () => obs.unobserve(blah) }, [obs, blah])
Had you not been using class components and thinking about lifecycles it would be clearer. function Thing({ stuff }) { // what people intuitively think you can do document.title = stuff // as close as possible to that useEffect(() => document.title = stuff) }
I need to frame this
Ah ha! This is it @dan_abramov, isn't it? This is the way to teach functional components and hooks outside of the lifecycle mental model! And it seems pretty intuitive after learning about data management and data flow.
We’re writing an article right now about this idea, hope to publish next week.
But this could be misleading. It should have all deps, not only state. E.g. if you need to call a Redux action, a callback function from the parent component, etc.
state is general here, doesn’t have to be from useState or useReducer, could be props, derived values, could be functions, a button’s click handler, whatever. “The state of the component when rendered”
Twitter je možda preopterećen ili ima kratkotrajnih poteškoća u radu. Pokušajte ponovno ili potražite dodatne informacije u odjeljku Status Twittera.