最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - onClick not firing in React js - Stack Overflow

matteradmin2PV0评论

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')
);
Post a comment

comment list (0)

  1. No comments so far