React JS onClick event handler

前端 未结 11 1129
孤街浪徒
孤街浪徒 2020-11-28 21:35

I have

var TestApp = React.createClass({
      getComponent: function(){
          console.log(this.props);
      },
      render: function(){
        retur         


        
11条回答
  •  一整个雨季
    2020-11-28 21:39

    Two ways I can think of are

    var TestApp = React.createClass({
        getComponent: function(index) {
            $(this.getDOMNode()).find('li:nth-child(' + index + ')').css({
                'background-color': '#ccc'
            });
        },
        render: function() {
            return (
                
    • Component 1
    • Component 2
    • Component 3
    ); } }); React.renderComponent( , document.getElementById('soln1'));

    This is my personal favorite.

    var ListItem = React.createClass({
        getInitialState: function() {
            return {
                isSelected: false
            };
        },
        handleClick: function() {
            this.setState({
                isSelected: true
            })
        },
        render: function() {
            var isSelected = this.state.isSelected;
            var style = {
                'background-color': ''
            };
            if (isSelected) {
                style = {
                    'background-color': '#ccc'
                };
            }
            return (
                
  • {this.props.content}
  • ); } }); var TestApp2 = React.createClass({ getComponent: function(index) { $(this.getDOMNode()).find('li:nth-child(' + index + ')').css({ 'background-color': '#ccc' }); }, render: function() { return (
    ); } }); React.renderComponent( , document.getElementById('soln2'));

    Here is a DEMO

    I hope this helps.

提交回复
热议问题