How to use setState to change a particular style attribute


style: {top: Math.ceil(10 * Math.random()+ (8*Math.random())),left: Math.ceil(150 * Math.random())/2,backgroundColor: ‘yellow’}

I want to change just the backgroundColor to red using this.setState without altering top and left.

Hi @davidradon

You could use Object.assign:

this.setState(state => 
  ({ style: Object.assign(state.style, { backgroundColor: 'red' } }))

@joesmith100 thanks, dat helped :wink:

there ws a typo in your code and i’m correcting it below

this.setState(state =>
({ style: Object.assign(state.style, { backgroundColor: ‘red’ }) }))

Oh yea, my bad. Thanks :smiley: