javascript - 如何在另一个组件中使用反应类属性
问题描述
我在反应组件( 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>
....
}
如何做到这一点?
解决方案
将handleClick函数作为道具传递给另一个组件:如:
<Component handleClick={this.handleClick} />
然后在您的按钮代码中: this.props.handleClick('next')} className='next'>ادامه
推荐阅读
- swift - Armadillo C++ 库 - C++11 编译器需要错误
- sql - 将 csv 文件从 postgresql 数据库服务器导出到本地计算机
- python - 忽略列表中的 none 并在 python 中取最小值和最大值
- javascript - 传递给函数时,对象索引数组被打乱
- r - R翻译curl命令以发布多部分形式 - 问题
- javascript - DOM 无法识别“InseartAdjacentHTML”
- node.js - Angular 10 的 JSReport 新手
- python - 将 3D 笛卡尔数据插值到球面
- deep-learning - 为什么我使用 cpu 而不是 cuda 在 colab 中运行脚本?
- python - python 国际象棋模块在google colab上无法正常工作