最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - onClick not firing in React js - Stack Overflow

programmeradmin2浏览0评论

I'm having troubles making this code work. When i click on the button, the ponent should re render and bee a h1 tag. This code piles, the problem is that when I click the button "Open Modal" nothing happens.

This is the last version i have.I'm fairly new to react.js so apologies in advance for my ignorance. Any ideas?

class AddTransactionForm extends React.Component {

      constructor (props) {
        super(props);
        this.state = { modalActive: false };
        this.openModal = this.openModal.bind(this);
      }

      openModal () {
        this.setState({ modalActive: true })
      }

      render () {
        if (this.state.modalActive){
          return(<h1>Active</h1>);
        }else{
          return (
                 <div>
                       <button className="button_" onClick={this.openModal}>Open modal</button>
                 </div>
        );
        }
    }
    }

Aditionaly, im getting this error in the console (although the error, the page renders and it's working fine)

Uncaught Error: Cannot find module "./ponents/Transactions" at bundle.js:17 at Object. (bundle.js:17) at t (bundle.js:1) at Object. (bundle.js:17) at t (bundle.js:1) at Object. (bundle.js:1) at t (bundle.js:1) at bundle.js:1 at bundle.js:1

I'm having troubles making this code work. When i click on the button, the ponent should re render and bee a h1 tag. This code piles, the problem is that when I click the button "Open Modal" nothing happens.

This is the last version i have.I'm fairly new to react.js so apologies in advance for my ignorance. Any ideas?

class AddTransactionForm extends React.Component {

      constructor (props) {
        super(props);
        this.state = { modalActive: false };
        this.openModal = this.openModal.bind(this);
      }

      openModal () {
        this.setState({ modalActive: true })
      }

      render () {
        if (this.state.modalActive){
          return(<h1>Active</h1>);
        }else{
          return (
                 <div>
                       <button className="button_" onClick={this.openModal}>Open modal</button>
                 </div>
        );
        }
    }
    }

Aditionaly, im getting this error in the console (although the error, the page renders and it's working fine)

Uncaught Error: Cannot find module "./ponents/Transactions" at bundle.js:17 at Object. (bundle.js:17) at t (bundle.js:1) at Object. (bundle.js:17) at t (bundle.js:1) at Object. (bundle.js:1) at t (bundle.js:1) at bundle.js:1 at bundle.js:1

Share Improve this question edited Jan 4, 2017 at 17:33 jmo66 asked Jan 4, 2017 at 16:53 jmo66jmo66 131 gold badge1 silver badge4 bronze badges 10
  • Have you checked by inspecting the html dom ? – Prasanna Mahendiran Commented Jan 4, 2017 at 16:56
  • Should work. Something else is broken. – Sergiu Paraschiv Commented Jan 4, 2017 at 16:58
  • Try wrapping your <h1> in a <div>. – jmargolisvt Commented Jan 4, 2017 at 17:00
  • @jmargolisvt why would that matter? – Martin Dawson Commented Jan 4, 2017 at 17:03
  • @SergiuParaschiv where should i look for errors? – jmo66 Commented Jan 4, 2017 at 17:16
 |  Show 5 more ments

1 Answer 1

Reset to default 1

This works perfectly fine. See the demo at http://codepen.io/umgupta/pen/dNPPXe Looking at the error it seems like you are using a bundler. The issue might be that since the error might be throwing before the events bind, on click didn't bind.

class AddTransactionForm extends React.Component {

      constructor (props) {
        super(props);
        this.state = { modalActive: false };
        this.openModal = this.openModal.bind(this);
      }

      openModal () {
        this.setState({ modalActive: true })
      }

      render () {
        if (this.state.modalActive){
          return(<h1>Active</h1>);
        }else{
          return (
                 <div>
                       <button className="button_" onClick={this.openModal}>Open modal</button>
                 </div>
        );
        }
    }
    }


ReactDOM.render(
  <AddTransactionForm/>,
  document.getElementById('root')
);
发布评论

评论列表(0)

  1. 暂无评论