I am fetching data from MongoDB in my ReactJS app using context API. When I log the data to the web page, I get three arrays instead of one. I am unsure why this is happening.
Here is my code:
In RoomProvider.js file:
I have created a context using React.createContext. In the constructor of RoomProvider, I have initialized state with users, rooms, sortedRooms, featuredRooms, and loading. I am using axios to fetch data from MongoDB in two separate methods - getRooms() and getUsers(). I have a getRoom() method that returns a specific room. In componentDidMount(), I call getRooms() and set loading to false. In the render() method, I am providing the state and getRoom() method to the context provider. In RoomContainer.js file:
I am consuming the context using the withRoomConsumer() higher-order component. I am logging the rooms array to the console. When I run the app, the console shows me an empty array being logged three times, instead of one array. I am unsure why this is happening.
Can someone please help me understand why I am seeing an empty array being logged three times? Here is my code:
(Include the code for RoomProvider.js, RoomContainer.js, and any other relevant files.)