Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

136
Views
Creating an Observer component in React - Passing props to the children

I'm looking to build an intersection observer component in a personal Gatsby project. The reason for it is the animation and trigger is the same in different areas of the site. What I have so far:

// Observer.js

import React, { useEffect, useRef } from "react";
import { useAnimation } from "framer-motion";

const Observer = ({ children }) => {
  const controls = useAnimation();

  const ref = useRef();

  useEffect(() => {
    const observer = new IntersectionObserver(
      ([entry]) => {
        if (entry.isIntersecting) {
          controls.start({
            opacity: 1,
            transition: { duration: 0.75, delay: 0.75 },
          });
        }
      },
      {
        root: null,
        rootMargin: "0px",
        threshold: 0.1,
      }
    );
    if (ref.current) {
      observer.observe(ref.current);
    }
  }, [ref]);

  return <div ref={ref}>{children}</div>;
};

export default Observer;

What I am looking to do is access controls in the children of the Observer.js component.

// ChildComponent.js

const ChildComponent= (props) => {

  return (
    <Observer>
      <div>
        <motion.div
          initial={{ opacity: 0 }}
          animate={props.controls}
        >
          <h2>This will animate on scroll</h2>
        </motion.div>
      </div>
    </Observer>
  );
};

export default ChildComponent;

I have seen answers such as - Pass props from layout to children in Gatsby - but I get the error props.children is not a function when attempting this.

Is what I am looking to do possible, or is there a better way of achieving this?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!