0

I am trying below code which i picked from stack only i am using this code to change color of selected item its working fine but i also want to set color of first element always selected once page load

                        var List = React.createClass({
                          getInitialState: function(){
                            return { active: null}
                          },

                          toggle: function(position){
                            if (this.state.active === position) {
                              this.setState({active : null})
                            } else {
                              this.setState({active : position})
                            }
                          },

                          myColor: function(position) {
                            if (this.state.active === position) {
                              return "blue";
                            }
                            return "";
                          },

                          render: function () {
                            return (
                              <div>
                                <li style={{background: this.myColor(0)}} onClick={() => {this.toggle(0)}}>one</li>
                                <li style={{background: this.myColor(1)}} onClick={() => {this.toggle(1)}}>two</li>
                                <li style={{background: this.myColor(2)}} onClick={() => {this.toggle(2)}}>three</li>
                              </div>
                            );
                          }
                        });
                        ReactDOM.render(
                            <List/>,
                            document.getElementById('app')
                        );
1

Can you do this?

 getInitialState: function(){
                    return { active: 0}
                  }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.