const addvariableSlider = [
'Discount Rate',
'Total Volume',
'R&D'
]
class HelloMessage extends React.Component {
dis(){
return (
< Range
value={this.state.discountRateRange}
onChange={this.discountChange}
onAfterChange={this.discountAfterChange}
/>
)
}
totVol(){
return (
< Range
value={this.state.totalVolRateRange}
onChange={this.totalVolChange}
onAfterChange={this.totalVolAfterChange}
/>
)
}
onSliderVarClick = (e) => {
if (e.value == "Discount Rate") {
this.dis()
}
if(e.value=="Total Volume"){
this.totVol();
}
}
render() {
return (
<div>
<Dropdown options={addvariableSlider} onChange={this.onSliderVarClick} value="" placeholder="Add Variable" /><br />
</div>
);
}
}
无论我基于下拉值调用的函数是什么,如何在render函数中动态显示那些返回的组件?在渲染中显示后,我可以处理onchange和onAfterChange事件
答案 0 :(得分:3)
这是正确的方法。
这些要注意的地方:
arrow
函数,以摆脱绑定对象。然后 尝试此更新的答案
class HelloMessage extends React.Component {
discountChange = () => {
alert('onChange called')
}
add = () => {
return (< Range
value={this.state.discountRateRange}
onChange={this.discountChange}
onAfterChange={this.discountAfterChange}
/>
)
}
render() {
return (
<div>
{this.add()}
</div>
);
}
}
答案 1 :(得分:1)
继续@Harish Soni的逻辑,此示例简化了代码并使代码更接近工作组件:
class HelloMessage extends React.Component {
constructor(){
this.state= {
discountRateRange = 'something'
}
}
discountChange = () => {
alert('discountChange called')
}
discountAfterChange = () => {
alert('discountAfterChange called')
}
render() {
return (
<div>
<Range
value={this.state.discountRateRange}
onChange={this.discountChange}
onAfterChange={this.discountAfterChange}
/>
</div>
);
}
}