Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ReactJS & Google MDL Button onClick not working

I have some JS like below. I find that if I remove mdl-js-layout the onClick of the button works. Otherwise it fails. Why might this be? I have already did componentHandler.upgradeDom()

'use strict';

module.exports = React.createClass({
  componentDidMount: function() {
    console.log('update')
    componentHandler.upgradeDom();
  },

  addExpense: function() {
    console.log('add expense');
  },

  render: function() {
    return (
      <div ref="appLayout" className="mdl-layout mdl-js-layout mdl-layout--fixed-drawer">
        <div className="mdl-layout__drawer">
          <span className="mdl-layout-title">Expenses</span>
          <nav className="mdl-navigation">
            <a className="mdl-navigation__link" href="#">Expenses</a>
            <a className="mdl-navigation__link" href="#">Settings</a>
          </nav>
        </div>

        <main className="mdl-layout__content">
          <div className="page-content">
            <div className="mdl-grid">
              <div className="mdl-cell mdl-cell--12-col">
                <button ref="btnAddExpense" onClick={this.addExpense} className="mdl-button mdl-js-button mdl-button--raised mdl-button--accent">
                  Add Expense
                </button>
              </div>
            </div>
          </div>
        </main>
      </div>
    );
  }
});

If I look into the React debugger tools, I can actually see the onClick is supposed to be bound?

enter image description here

like image 264
Jiew Meng Avatar asked Mar 27 '16 14:03

Jiew Meng


People also ask

What is React JS used for?

The React. js framework is an open-source JavaScript framework and library developed by Facebook. It's used for building interactive user interfaces and web applications quickly and efficiently with significantly less code than you would with vanilla JavaScript.

Is React JS frontend or backend?

js: React is a declarative, efficient, and flexible JavaScript library for building user interfaces. ReactJS is an open-source, component-based front-end library responsible only for the view layer of the application. It is maintained by Facebook. Moreover, React Js makes Front-end development very easy.

Is React for HTML or js?

To get an overview of what React is, you can write React code directly in HTML. But in order to use React in production, you need npm and Node.js installed.

Is React JS frontend?

React JS, commonly referred to as React is one frontend development framework, or to be more specific a library that has found a favorite with developers around the world.


1 Answers

It looks like mdl is not compatible with react, and prevents the click events from the react component from working. Hopefully, this would be fixed in a future update, but until then, you can work around this by manually re-adding the events after mounting the component.

componentDidMount: function() {
  componentHandler.upgradeDom();
  var button = this.refs.btnAddExpense;
  button.addEventListener('click', this.addExpense);
},

Demo

like image 129
James Brierley Avatar answered Oct 06 '22 00:10

James Brierley