4 ms·
How would you pass values between two different Hooks? See my two examples in the post (useSpring and useFriendStatus). Note they need to be always up-to-date
by danabramov 8y ago
How would you pass values between two different Hooks? See my two examples in the post (useSpring and useFriendStatus).
Note they need to be always up-to-date and not just execute once.
- sfvisser 8y agoWith passing values between hooks do you mean "Make one hook depend on the value of another hook?". Because that seems like the functor 'map', or applicative 'apply'. Note, I don't really know what these springs are, but derived hooks could work like this: class MyComp extends ReactishComponent { fast = this.use(new Spring({ pos: [0, 0], config: "fast" })) // prim hook slow = this.use(this.fast.map(spring => ({ pos: spring.pos, config: "slow" }))) // functor map mid = this.use(combineTwoHooks((fast, slow) => computeMiddle(fast, slow), this.fast, this.slow)) // applicative render() { return ( <> <Point pos={this.fast.get().pos} /> <Point pos={this.slow.get().pos} /> <Point pos={this.mid.get().pos} /> </> ) } } The trick is functions that convert hooks into new hooks.