Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

140
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda