How to use setState to change a particular style attribute

How to use setState to change a particular style attribute
0

#1


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.


#2

Hi @davidradon

You could use Object.assign:

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

#3

@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’ }) }))


#4

Oh yea, my bad. Thanks :smiley: