score:2
Accepted answer
whats wrong is that you are directly mutating
the state and it doesn't re-render so the change is not reflected in your dom. use setstate
to update state
componentwillmount() {
booksapi.getall().then((books) => {
const tempbooks = [];
books.foreach((book) => {
if (this.state.shelf === book.shelf){
// console.log(book);
tempbooks.push(book);
}
})
this.setstate(prevstate => ({books: [...prevstate.books, ...tempbooks]}))
})
}
score:0
this is the correct fix. i want to add the book into the previous array.
componentwillmount() {
booksapi.getall().then((books) => {
books.foreach((book) => {
if (this.state.shelf === book.shelf) {
this.setstate(prevstate => ({books: [...prevstate.books, book]}))
}
})
})
}
score:1
render() {
let books = this.state.books;
return (<div classname="bookshelf">
<h2 classname="bookshelf-title">{this.titles[this.props.shelf]}</h2>
<div classname="bookshelf-books">
<ol classname="books-grid">
{books.map((book) => return(<book/>))}
</ol>
</div>
</div>)
}
Source: stackoverflow.com
Related Query
- React - Adding component in Map function not adding
- React stateless component where map function is not returning
- Map function not working while passing props into React component (TypeScript, React and Next.js)
- React map function does not execute when the component is rendered
- React component not getting rendered with map function
- Map function not working in React js component
- "this.props" not working in my on mouseup event function in Map Component using react and mapbox-gl
- react js map function can not render the custom component
- React - my map function on my array component is not defined when I try to declare it somewhere else
- map function not working in React
- how to access vairables outside of map function in js and jsx in a React component
- Using JS React component in TypeScript: JSX element type 'MyComponent' is not a constructor function for JSX elements
- Uncaught TypeError: *** is not a function in a React component
- _this.store.getState is not a function when testing react component with enzyme and mocha
- Jest onSpy does not recognize React component function
- React Arrow Function Component - setState is not defined
- React - State Hook map is not a function
- React - State Hook map is not a function
- Simple react component not accepting arrow function
- How to use ES6 map function in React stateless component
- Adding a class to first n elements in React map function dynamically
- Axios not updating state from API in function called on Click, but works on identical function on Component Mount in React
- Arrow function "this" binding not working in React component
- React HOC has a Typescript error as a class component but not as a function component
- Webpack with babel-loader not able to parse react function component
- Component not mounted when setState called in arrow function in React with ES2016
- react map is not a function
- adding key to map function is not working
- Map function in react (err: TypeError: e.map is not a function)
- React function not passing down from functional to functional component
More Query from same tag
- Put results of a Get request from axios to an array
- Get index value from function calling in onChange input element
- Get an element created by a script in react without ref
- how to run 1 Express server project multiple time and with diferent .env file
- How to increase Chart.js yAxes' height to prevent overlapping
- React Bootstrap Navbar Routing
- React: Pass image from this.state in parent component to child component
- Clock is not ticking with sample time input (without getting system time)
- How to override Next.js `*.svg` module declaration?
- Comparing PrevProps in componentDidUpdate
- How to pass data in react js inside a file
- NextJs: Calling setInterval with a variable for time inside of a React useEffect Hook
- Creating a reusable component reactjs
- React won't draw anything in the canvas in useEffect
- setState(): Do not mutate state directly. Use setState()
- How to fix wrong alignment of logo in navigation bar?
- Map() returning duplicate component
- React JS Label Enable display
- RTK Query with AppSync returns status "rejected"
- How do you change the switch label when the state changes in Material UI?
- React conditional rendering not working as expected
- Axios post method in react isn't working: suggestions?
- connect not change the props of child component when the prop object modified
- Why Twitter Typeahead doesn't work with React js?
- React.js - Controlled input toLocaleString()
- Execution context does not move from getStaticPaths to getStaticProps as it normally does
- NEXTJS Sending formdata to api endpoint - no response
- Interpolate a dynamic HTML element in React
- React components in existing application
- React component with dynamically created function