首页 > 解决方案 > 如何在另一个组件中使用反应类属性

问题描述

我在反应组件( Createjob.js )中有一个属性:

class Createjob extends Component {
.....
  handleClick = clickType => {
    const {currentStep} = this.state
    let newStep = currentStep
    clickType === 'next' ? newStep++ : newStep--
    if (newStep > 0 && newStep <= 6) {
      this.setState({
        currentStep: newStep
      });
    }
  }
}
export default createjob

我在另一个反应组件中有一个按钮,我希望它使用这个属性,比如:

export class MainInfo extends Component {
<button type='submit' onClick={() => this.Createjob.handleClick('next')} className='next'>ادامه</button>
....
}

如何做到这一点?

标签: javascriptreactjspropertiesjsx

解决方案


handleClick函数作为道具传递给另一个组件:如:

<Component handleClick={this.handleClick} />

然后在您的按钮代码中: this.props.handleClick('next')} className='next'>ادامه


推荐阅读