Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

142
Views
setState in react it seems that it delete all state content

I wrote an object style and I was calling it in style of any element. It was cool tell i wanted to edit one style value of some element, so I tried to change it like

onClick={{cardStyle.li.backgroundColor : "black }}

But that gave me a read only error, so I put the cardStyle object in state in order using setState, and now when i try to change anyvalue using setState, it seems that it only puts that value and deleted everything from it

This is the code:

    import React from "react";
    
    
    export default class CardInfo extends React.Component
    {
        constructor(props){
            super(props);
    
            this.state = { cardStyle : {
                ul : {
                    width         : '80%',
                    margin        : '5% 10%',
                    listStyleType : "none", 
                },
                li :{
                    width         :  "30%",
                    border        : " 4px solid rgb(0, 0, 0 , 0.6) ",
                    borderRadius  : "7%",
                    textAlign     : "center",
                    paddingTop    : "3%",
                    marginRight   : '1%',
                    float         : 'left',
                    backgroundColor : this.props.Fname === "MUHAMMAD"  ? "rgb(255, 10, 92 , .7)" : "white",
                },
                img : {
                    width : " 80%",
                    borderRadius  : "7%",
                    
                },
                on : {backgroundColor : "black"}
            }}
        }
        render()
        { 
            
        
            
            return <ul  style={this.state.cardStyle.ul}>
                     <li style={this.state.cardStyle.li} 
                                onClick={() =>this.setState({cardStyle:{li:{backgroundColor : "grey"}}})} >
                        <img style={this.state.cardStyle.img} src={this.props.img} alt="lilpic" />
                        <h2>{this.props.Fname}</h2>
                        <p>{this.props.quetes}</p>
                        <p>{this.props.phone}</p>
                     </li>
    
                </ul>
    
        
        }
    }

after click

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You are changing state and overriding it with new values, instead you should override the property you want and give rest of the properties as well. So replace your on click this.setState with

this.setState({cardStyle:{ ...this.state.cardStyle, li:{backgroundColor : "grey"}}})} >

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!