score:1
With set.state.PostsAtt.push
, you are mutating the state. Also you are setting the state inside the for loop.
Use setState callback back approach to set the state and make sure to append the data and update the state after for loop.
Like this
InfScroll = () => {
let db = firebase.firestore();
let set = this;
let lastVisible;
var first = db
.collection("newPost")
.orderBy("createdAt", "desc")
.limit(set.state.PostsAtt.length);
return first.get().then(function (documentSnapshots) {
lastVisible = documentSnapshots.docs[documentSnapshots.docs.length - 1];
var next = db
.collection("newPost")
.orderBy("createdAt", "desc")
.startAfter(lastVisible)
.limit(3);
next.get().then((documentSnapshots) => {
const postAttArr = []; //<----see here
documentSnapshots.forEach((doc) => {
postAttArr.push({ data: doc.data(), id: doc.id });
});
set.setState(prev => ({ PostsAtt: [...prev.PostsAtt, ...postAttArr] })); //<----see here
});
});
};
Side note:
If you don't wan't to deal with things like let set = this;
, you can replace return first.get().then(function (documentSnapshots) {
with an arrow function
Like this
...
var first = db
.collection("newPost")
.orderBy("createdAt", "desc")
.limit(set.state.PostsAtt.length);
return first.get().then((documentSnapshots) => { //<---- here
lastVisible = documentSnapshots.docs[documentSnapshots.docs.length - 1];
...
score:1
I believe the problem is over here.
set.setState( { PostsAtt: set.state.PostsAtt.push({ data: doc.data(), id: doc.id }) } );
Array.push returns the number of items you have on the array after you push. You should push the new data outside setState, or use spread operator
const newPosts = [...set.sta.PostsAtt, { data: doc.data(), id: doc.id }]
set.setState({ PostsAtt: newPosts });
Source: stackoverflow.com
Related Query
- React - State Hook map is not a function
- React - State Hook map is not a function
- How to update state in map function in reactjs
- state.get is not a function when trying to map the react-redux State
- Uncaught TypeError: map is not a function In Reactjs with Firebase
- setState in reactjs inside success function not setting state
- map is not a function when updating state
- set state in a function not working - reactJs
- React why is this simple array map function not returning anything?
- ReactJS this props is not a function
- map is not a function in reactJS
- map function does not return anything in reactjs
- this.state array in reactjs map is not a function
- ReactJS useGesture: typeerror set is not a function
- ReactJS map function not working on a lightbox gallery
- I am not able to add user input to state properly, getting map not a function error when it is
- Reactjs map function does not render the component
- Map function not able to implement in reactJS app
- Completely stumped at this reactjs logic, of a variable not updating the state of a component
- reactjs updating parent state from child in map function
- ReactJS why will this state change not render? What am I doing wrong?
- reactjs - this is not a function error on upgrade
- map function is not iterating the state objects(in array format) in react.js
- Reactjs filter then map function isn't display images? Is it not re-rendering?
- map() function in Reactjs is not working with state
- Images are not displaying with <li> tag in map function for array object in Reactjs
- ReactJS not binding via map to state values
- reactjs - Function not working inside a map function
- Why does adding/removing curly braces in this arrow function cause the text to not display for a ReactJS component?
- Is this method a proper way to avoid .map is not a function in ReactJS
More Query from same tag
- fetch wrapped in request.js in react boilerplate: how to handle error from json body for Spring backend
- Adding variable to script tag in React
- React Router - What's the difference between Router and Switch?
- Admin-on-rest - Place pagination at top
- How to call a function if Current position within Polygon in google-,maps-react?
- Why doesn't onClick work on custom component in react?
- How to pass a prop to my child route?
- With React, is it possible to have a propType to specify the DOM element type?
- Parsing error: JSX element 'ValidatedForm' has no corresponding closing tag
- Using React i18next do I have to use withTranslation?
- how to properly iterate through array of post objects retrieved through Wp Rest Api in react js
- jest onChange Unit test - React
- Removing incomplete data from being displayed on the web
- ES6+ / React Native class properties: The first one is undefined
- Invalid prop `selectRow` of type `string` supplied to `BootstrapTable`, expected `object`
- Not able to change the state of a CheckBox in React Native
- TypeError: Cannot destructure property 'enqueueSnackbar'
- Webpack - Uncaught SyntaxError: Unexpected token import
- How to toggle the arrows when children is expanded / collapsed in 'cp-react-tree-table'
- React Konva Blend Modes
- How to make the entries in one column clickable in React Table?
- react-router (v4) how to go back?
- Unable to type in textbox after editing a value in React
- React-Select Duplicates and Use Case
- how to add JS file in Node Js
- Very simple state change of specific array item in React
- How do you get subpaths to work for create-react-app in minikube?
- React Native - How to prevent full screen video mode in IOS?
- Full-fledged PDF.js viewer in React
- Passing parameters to jest function