Search code examples
reactjsreact-hookskonvajskonva

how initialize third party library (konvajs) in react hooks


I try to initialize third party library In React hooks I'm using useEffect but I get All the time null I tried using custom hooks but I get the same result (null)

any idea?

this my code https://stackblitz.com/edit/react-rg9uov

thank's


Solution

  • Your useEffect should declare dependency array for ref, also not sure why const stage = useRef(null); is used? stage could be just a component state.

     const [stage, setStage] = React.useState();
     const ref = useRef();
    
     useEffect(() => {
        if(ref.current){
          setStage(new Konva.Stage({
          container: ref.current, // id of container <div>
          width: 500,
          height: 300
        }))
        }
    
      },[ref]);