score:3
Accepted answer
const updateBubble = ({y, vy, ...props}) => ({y: y + vy, vy: vy + 0.1, ...props})
this.setState(state => ({bubbles: state.bubbles.map(updateBubble)}))
score:0
import React from 'react'
import Box from './box'
export default class Boxes extends React.Component {
constructor(props) {
super(props);
this.state = {
started:[false,false,false,false] /*these flags will be changed on your "onClick" event*/
}
}
render() {
const {started} = this.state
return(
<div>
{started[0] && <Box />}
{started[1] && <Box />}
{started[2] && <Box />}
{started[3] && <Box />}
</div>
)
}
}
score:2
this.state = {bubbles: history}
this.setState({ bubbles: this.state.bubbles.map(x => {
// Option 1 - more verbose
let newBubble = x; // or even let newBubble = {...x}
newBubble.vy = x.vy + 1;
// Option 2 - directly update x
x.vy = x.vy + 1
// Then
return x;
})})
Source: stackoverflow.com
Related Query
- Using setState to change multiple values within an array of objects -- ReactJS
- Merge properties of objects within a array together using values and remove duplicate
- Change a property of one object in an array of objects using setState (React)
- How to store multiple values in to objects within an array
- reactJs setState change property on object within an array within an array
- reactJs setState change property on object within an array
- Push multiple objects with variable keys to array using setState (react-select)
- How to append multiple values in setstate for array object Reactjs
- is there a way to filter an array of objects based on multiple checkbox values on checked change
- How to update an object in an array of Objects using setState
- Displaying array of objects using map in reactJs
- I want to save all Button values in array using ReactJs
- How can I send a file within an array of objects using formData.append()
- Returning values from an array in reactjs without using this.state
- ReactJS useState hook: Can I update array of objects using the state variable itself?
- React how to conditionally edit or add object in array of objects using setState
- Using values from an array using useEffect giving multiple arrays
- How to sum, divide values of multiple inputs using onChange event | ReactJS
- How can I add checkbox values to setState array in reactJS
- Using a value to compare against an array of objects to return a different value within that object?
- Add multiple values from checkbox to array of objects
- How to change values in array of objects with an onClick?
- how to change value of object in array using setState with React
- How to change only one object in a large state array of objects using Hooks?
- React/Javascript - Render a List of Values Based On Same Object Keys Within an Array of Objects
- How to change individual elements within array using useState
- Change multiple key values and setState in React
- How to validate array of objects using reactjs
- Javascript filter Array of objects with multiple conditions and values
- Using setState to dynamically populate an array of objects
More Query from same tag
- Promise only resolves correctly on page refresh
- Is there a way to reuse Bootstrap's OverlayTrigger component?
- Set the initial value of the clock and setInterval () based on the seconds contained in the object
- Can I create a file with functions only and import it in an App when using React?
- Why create-react-app alias is not able to find index.js from folder?
- How to update a react-chartjs-2 plot using useState()
- Using aria attributes on elements in react
- How to filter an array and remove duplicated elements?
- Is passing stringified object as prop in React component better than plain JavaScript object?
- Dynamic Form in React with Redux-Form
- What is the intention of using React's useCallback hook in place of useEffect?
- Redux-persist for reactJS giving error : noop storage in Firefox 78.0.2
- React.Js 0.14 - Invariant Violation: Objects are not valid as a React child
- Using @material-ui/core, how do I make a Slider where the track is divided into left and right with different colors?
- reseting or clearing an input field with reactjs
- What is difference between React vs React Fiber?
- How to use React Router's Link with a variable pathname?
- fetch data react .map is not a function
- access array member of type object in javascript
- How to make MUI's Autocomplete display the label from the matching value in props.options?
- How to change the UI state of a group of Checkboxes with Formik
- How to get multiple values from child component in Reactjs?
- ReactJS: How do I open an Material-UI Accordion programmatically?
- I want to change price depending on checkboxes ticked. How to remove if statement from onSubmit()
- Setting state returns returns undefined, Cannot destructure property ‘title’ of ‘undefined’ as it is undefined
- Cannot load ES6 module with Webpack
- how to add only attribute on tag inside map javascript
- Issue when implementing ReactDOM.createPortal (react 16 feature)
- Element type is invalid: expected a string (...) but got: object
- Inifinite loop when saving an object from async await