Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use onClick with divs in React.js

I'm making a very simple application where you can click on square divs to change their color from white to black. However, I'm having trouble. I'd like to use the onClick function to allow a user to click on a square to change its color, but it doesn't seem to be working. I've tried using spans and empty p tags, but that doesn't work either.

Here is the code in question:

var Box = React.createClass({     getInitialState: function() {         return {             color: 'white'         };     },      changeColor: function() {         var newColor = this.state.color == 'white' ? 'black' : 'white';         this.setState({             color: newColor         });     },      render: function() {         return (             <div>                 <div                     style = {{background: this.state.color}}                     onClick = {this.changeColor}                 >                 </div>             </div>         );     } }); 

Here's a link to my small project on CodePen. http://codepen.io/anfperez/pen/RorKge

like image 463
Leia_Organa Avatar asked Nov 10 '16 19:11

Leia_Organa


People also ask

Can I give onClick to div?

We can bind a JavaScript function to a div using the onclick event handler in the HTML or attaching the event handler in JavaScript. Let us refer to the following code in which we attach the event handler to a div element. The div element does not accept any click events by default.

How do I render a div on click react?

To show or hide another component on click in React: Set the onClick prop on an element. When the element is clicked, toggle a state variable that tracks if the component is shown. Conditionally render the component based on the state variable.


1 Answers

This works

var Box = React.createClass({     getInitialState: function() {         return {             color: 'white'         };     },      changeColor: function() {         var newColor = this.state.color == 'white' ? 'black' : 'white';         this.setState({ color: newColor });     },      render: function() {         return (             <div>                 <div                     className='box'                     style={{background:this.state.color}}                     onClick={this.changeColor}                 >                     In here already                 </div>             </div>         );     } });  ReactDOM.render(<Box />, document.getElementById('div1')); ReactDOM.render(<Box />, document.getElementById('div2')); ReactDOM.render(<Box />, document.getElementById('div3')); 

and in your css, delete the styles you have and put this

.box {   width: 200px;   height: 200px;   border: 1px solid black;   float: left; } 

You have to style the actual div you are calling onClick on. Give the div a className and then style it. Also remember to remove this block where you are rendering App into the dom, App is not defined

ReactDOM.render(<App />,document.getElementById('root')); 
like image 127
Komolafe Tolulope Avatar answered Sep 26 '22 03:09

Komolafe Tolulope