javascript - Color toggle not working as intended using react states -


i'm trying toggle background color of 4 divs i'm mapping according database contents. (this poll has 4 options)

i'm getting voted option when component first renders , using states i'm setting background color div matches voted option id.

and when clicking on new div want set background color div while removing background color of voted option. able set background color newly selected option cannot remove background color of voted option.

my approach this

  class textvoteoptions extends component {      constructor(props) {          super(props);          this.state = {              votecount: this.props.contents.vote_number,             active: true,             voted_id: this.props.voted_id          }          this.handleclick = this.handleclick.bind(this);      }      handleclick(id) {           this.setstate({             voted_id: id,             active: (id != this.state.voted_id) ? true : !this.state.active         })       }      render() {          let {contents,voted_id} = this.props          return (              <div key={this.props.contents.post_poll_content_id}                  classname={(this.state.voted_id === this.props.contents.post_poll_content_id && this.state.active) ? "txt_vote_bar_div active" : "txt_vote_bar_div"}>                 <p classname="txt_vote_choice" id={this.props.contents.post_poll_content_id}                    onclick={() => {                        this.handleclick(this.props.contents.post_poll_content_id);                    }}>                     {contents.content}                 </p>                 <p classname="txt_tot_votes"> {this.props.contents.votes_percentage}%                     ({this.state.votecount} votes)</p>             </div>         );     };  } 

this.props.voted_id voted option id i'm getting when component renders.

i'm facing problem of not being able remove background color of voted option. doing wrong here?

my css

.txt_vote_bar_div {      display: table;     width: 100%;     height: 50px;     border: 1px solid #c6c6c6;     border-radius: 5px;     margin-bottom: 10px;     cursor: pointer;     transition: 1s;     -webkit-transition: 1s;     -moz-transition: 1s;  }  .active {     background-color:#0b97c4;     color:#ffffff; } 


Comments

Popular posts from this blog

python - Operations inside variables -

Generic Map Parameter java -

arrays - What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it? -