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?
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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With