Using document.querySelector in React? Should I use refs instead? How?

I can’t answer the “should you” part of whether to use refs for this instead other than if you do, you don’t need those id values unless you use them for something else. But here’s how you would: Use useRef(null) to create the ref. const activeSlideRef = useRef(null); Put it on the Slide that’s currently … Read more

How to setInterval for every 5 second render with React hook useEffect in React Native app?

You need to clear your interval, useEffect(() => { const intervalId = setInterval(() => { //assign interval to a variable to clear it. setState(state => ({ data: state.data, error: false, loading: true })) fetch(url) .then(data => data.json()) .then(obj => Object.keys(obj).map(key => { let newData = obj[key] newData.key = key return newData }) ) .then(newData => … Read more

Stop useEffect from running on mount

You can’t configure it out of the box. But, a common pattern is to use some isMounted flag like so: // Is Mounted const useFetchNotOnMount = () => { … const isMounted = useRef(false); useEffect(() => { if (isMounted.current) { console.log(‘fetching’); dispatch(fetchData(filters)); } else { isMounted.current = true; } }, [dispatch, filters]); }; // Same … Read more