1

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

skyboyer
  • 22,209
  • 7
  • 57
  • 64
Alex Malin
  • 63
  • 5

2 Answers2

2

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]);
0

You're seeing null because the console.log is running before useEffect, if you put a console.log at the end of useEffect you'll see that Konva is initialized properly.

rzwnahmd
  • 1,072
  • 4
  • 8