score:2
You redeclare folderRef
each render cycle, so if you include it in the useEffect
hook's dependency array it will trigger render looping.
If you don't refer to folderRef
anywhere else in the component then move it into the useEffect
hook callback to remove it as an external dependnecy.
const [folders, setFolders] = useState([]);
useEffect(() => {
const folderRef = collection(db, "folders");
const getData = async () => {
const data = await getDocs(folderRef);
const folderData = data.docs.map((doc) => {
return { id: doc.id, data: doc.data() };
});
console.log(folderData);
setFolders(folderData);
};
getData();
}, []);
Or store it in a React ref so it can be safely referred to as a stable reference.
const [folders, setFolders] = useState([]);
const folderRef = useRef(collection(db, "folders"));
useEffect(() => {
const getData = async () => {
const data = await getDocs(folderRef.current);
const folderData = data.docs.map((doc) => {
return { id: doc.id, data: doc.data() };
});
console.log(folderData);
setFolders(folderData);
};
getData();
}, [folderRef]);
Update
I've gathered that you are updating the folders
collection elsewhere in your app and want this component to "listen" for these changes. For this you can implement an onSnapshot
listener.
It may look similar to the following:
const [folders, setFolders] = useState([]);
useEffect(() => {
const unsubscribe = onSnapshot(
collection(db, "folders"),
(snapshot) => {
const folderData = [];
snapshot.forEach((doc) => {
folderData.push({
id: doc.id,
data: doc.data(),
});
});
setFolders(folderData);
},
);
// Return cleanup function to stop listening to changes
// on component unmount
return unsubscribe;
}, []);
score:0
I think most Probably your useState function is like
const[folderRef , setFolders]=useState(Your Initial Value);
if this is the case then when ever you perform
useEffect(() => {
setFolder(Setting Anything Here)
....
},[folderRef])
React starts an infinity loop coz every time you use setFolder the FolderRef gets updated and the useEffect is forced to run again and it won't Stop .
use something like
const[folderRef , setFolders]=useState(Your Initial Value);
const[isLoading, setIsLoading]=useState(true);
useEffect(() => {
setFolder(Setting Anything Here)
setIsLoading(false)
....
},[])
...
return (
{ isLoading ? "Run the code you wanna run " : null }
)
Source: stackoverflow.com
Related Query
- useEffect rendering multiple times even when data is not changing
- enlighten me about UseEffect Loop when data not changing
- useEffect hook is turning into infinite loop even when the dependency is not changing all the time
- useState not rendering data even when api response is showing in console
- react redux useSelector rerender even when data does not change
- svg not rendering correctly when used with by multiple react components
- Prevent re-rendering when state is changed multiple times during useEffect
- Infinite rendering of component when fetching data in useEffect wrapped in HOC
- Why does setState Hook from React is rendering twice even not changing the state?
- How to prevent React Hook's useEffect from fetching data multiple times
- React.StrictMode: SetState function in useEffect is run multiple times when effect is run once
- Why does state not update correctly when calling setState() multiple times
- Async function not waiting for results even when used inside useEffect with await
- React component not re-rendering on URL parameter change when using useEffect hook to fetch data
- Why does optional chaining allows rendering when fetching data through useEffect in an app that uses context?
- my 'posts' state is not rendering even after getting data in console
- react when use useeffect for unmount , data not change
- Im trying to build a menu that has collapsible options, but the dom does not update even when state data updates
- Continuous Rendering even when value does not change
- react component not working properly when rendered multiple times
- useEffect not triggers when rendering a component
- React not rendering data from useEffect
- React Native FlatList not rendering when data updates (Redux)
- React functional component is not rendering when updating the Sate after pushing data to array
- api data not changing when url_slug is changing in react hook
- onChange is appending data multiple times when checking for a string character
- when my route is placed in child components url is changing but component is not rendering
- React useEffect to fetch data not finished yet when try to sort data using React Router useSearchParams
- Changing state even when the react-tabs component is not selected
- Why React.Component only rendering once even when I have multiple target elements
More Query from same tag
- D3.js V4 zoom doesn't work in React + faux
- Passing an attribute from a react component to a rails back end
- Change path in react router without causing re-render?
- Implementing jquery UI Datepicker inside Reactjs
- How to get cell value on React-Bootstrap-Table?
- ENOENT error when serving React App on Azure
- React firebase authentication returns null
- How to have a React child component call a function from a parent component
- Missing Something Sending Prop In Link In React
- JSON data not being sent - Django Rest Framework / React
- React Router: Cannot read property 'pathname' of undefined
- `npx create-react-app` says it requires "Node 14 or higher" but I'm running 17.4.0. How can I fix this?
- React JS bold the text in between
- Reactjs - Playing audio files from outer folder in local machine
- How to use a second query in a Promise in JS ? using mysql db
- pass state value as props to another component es6
- Want to reduce the unnecessary gaps between html tags
- addEventListener in a React app isn't working
- Unable to get redux state outside component
- react const contains undefined after setState with useImmer
- animate text in React
- Check for empty of null in javascript
- Firebase orderByChild not returning an array
- Call this.props inside map
- react js issue occurred with Link tag
- How can I change the focused color of a TextField?
- Jest/Enzyme unit testing
- error using dotenv-webpack with react project
- Is there a React equivalent of Vue's nextTick() function?
- React twice mount component, but on second time doesn't receive props