Code Monkey home page Code Monkey logo

Comments (2)

mephistia avatar mephistia commented on September 27, 2024

I believe I've figured it out:

const parallaxElements = parallaxController?.getElements();

const refId = parallaxElements?.find((entry) => entry.el.isEqualNode(ref.current))?.id || 0;

parallaxController?.updateElementPropsById(refId, newProps);

But it would be nice to update the docs informing where does this id come from

from react-scroll-parallax.

jscottsmith avatar jscottsmith commented on September 27, 2024

@mephistia

There's no need to do all this extra work. Just use the tools React gives you to pass in new values to the useParallax hook. It will do all this updating for you automatically.

See the example below, which adds an isMobile state which is then updated by the resize event listener. If isMobile is true, we pass a different set of scale values to the parallax hook.

function Example() {
  const [isMobile, setIsMobile] = useState(false);

  const { ref } = useParallax<HTMLDivElement>({
    // change any values based on React state
    scale: isMobile ? [2, 1] : [1, 2]
  });

  function handleResize() {
    if (window.innerWidth < MOBILE_MAX_WIDTH) {
      setIsMobile(true);
    } else {
      setIsMobile(false);
    }
  }

  useEffect(() => {
    window.addEventListener("resize", handleResize);
    return () => {
      window.removeEventListener("resize", handleResize);
    };
  }, []);

  return (
    <div className="parallax-element" ref={ref}>
  );
}

Here's the working CodeSandbox using the code example above.

Hope that helps.

from react-scroll-parallax.

Related Issues (20)

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.