javascript - onclick props not working with Material UI? - Stack Overflow

here is my situation on React.jsI have a function on my App.js call selectNumberOfPeople,then In my chi

here is my situation on React.js

I have a function on my App.js call selectNumberOfPeople,

then In my child ponent ( call General) I had a button as:

<button className="selectedNumberOfPeopleButton" onClick={this.selectedNumberOfPeople} value="1">
      1
</button>

which was displaying the value in the console on click.

Works perfectly.

I want to use a button from Material UI instead now, so I have replace my button with:

<RaisedButton className="selectedNumberOfPeopleButton" 
    onClick={this.props.selectedNumberOfPeople}
    value="1" 
    label="1" 
    labelPosition="before"
    primary={true}
/>

But the value doesnt display anymore int he console when using this . . .

though the function being in the parent ponent I do pass it by:

<General selectNumberOfPeople={this.selectNumberOfPeople} selectedPanel={this.showPanelAndHideOthers} />

and I tried to updated my child ponent ( General.js) like:

<RaisedButton selectNumberOfPeople={this.props.selectNumberOfPeople}
    className="selectedNumberOfPeopleButton" 
    onClick={this.props.selectedNumberOfPeople}
    value="1" 
    label="1" 
    labelPosition="before"
    primary={true}
/>

but it's still not working....

For your information,

the selectNumberOfPeople is in App.js as

selectNumberOfPeople(value) {
console.log('select number of people');
// update the state and provide a callback handler to be called after the state is updated.
// referencing the state before the call back function
// () => {
//   console.log(this.state.numberOfPeople)
// })
// will leave the state with the same value as before the setState function is called.
this.setState(
  {
    numberOfPeople: value,
  },
  () => {
    console.log(this.state.numberOfPeople);
  }
);

}

and in my general.js (child ponent)

selectedNumberOfPeople(e) {
  this.props.selectNumberOfPeople(e.target.value);

  const list = document.getElementsByClassName('selectedNumberOfPeopleButton');
  for (let i = 0; i < list.length; i++) {
    list[i].classList.remove('hover');
  }

  this.toggleSelectedButtonState(e);
}

Does anyone have any guidance in what I'm doing wrong ?

It will be super !!

Many thanks !

here is my situation on React.js

I have a function on my App.js call selectNumberOfPeople,

then In my child ponent ( call General) I had a button as:

<button className="selectedNumberOfPeopleButton" onClick={this.selectedNumberOfPeople} value="1">
      1
</button>

which was displaying the value in the console on click.

Works perfectly.

I want to use a button from Material UI instead now, so I have replace my button with:

<RaisedButton className="selectedNumberOfPeopleButton" 
    onClick={this.props.selectedNumberOfPeople}
    value="1" 
    label="1" 
    labelPosition="before"
    primary={true}
/>

But the value doesnt display anymore int he console when using this . . .

though the function being in the parent ponent I do pass it by:

<General selectNumberOfPeople={this.selectNumberOfPeople} selectedPanel={this.showPanelAndHideOthers} />

and I tried to updated my child ponent ( General.js) like:

<RaisedButton selectNumberOfPeople={this.props.selectNumberOfPeople}
    className="selectedNumberOfPeopleButton" 
    onClick={this.props.selectedNumberOfPeople}
    value="1" 
    label="1" 
    labelPosition="before"
    primary={true}
/>

but it's still not working....

For your information,

the selectNumberOfPeople is in App.js as

selectNumberOfPeople(value) {
console.log('select number of people');
// update the state and provide a callback handler to be called after the state is updated.
// referencing the state before the call back function
// () => {
//   console.log(this.state.numberOfPeople)
// })
// will leave the state with the same value as before the setState function is called.
this.setState(
  {
    numberOfPeople: value,
  },
  () => {
    console.log(this.state.numberOfPeople);
  }
);

}

and in my general.js (child ponent)

selectedNumberOfPeople(e) {
  this.props.selectNumberOfPeople(e.target.value);

  const list = document.getElementsByClassName('selectedNumberOfPeopleButton');
  for (let i = 0; i < list.length; i++) {
    list[i].classList.remove('hover');
  }

  this.toggleSelectedButtonState(e);
}

Does anyone have any guidance in what I'm doing wrong ?

It will be super !!

Many thanks !

Share Improve this question edited May 8, 2018 at 19:09 tibewww asked May 8, 2018 at 16:38 tibewwwtibewww 6034 gold badges13 silver badges33 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 1

Use this.props.selectNumberOfPeople not selectedNumberOfPeople.

 <RaisedButton
   className="selectedNumberOfPeopleButton" 
   onClick={this.props.selectNumberOfPeople}
   value="1" 
   label="1" 
   labelPosition="before"
   primary={true}
/>

you can also try onClick={()=>this.props.selectedNumberOfPeople}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745425672a4627162.html

相关推荐

  • javascript - onclick props not working with Material UI? - Stack Overflow

    here is my situation on React.jsI have a function on my App.js call selectNumberOfPeople,then In my chi

    4小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信