[英]How to use a react class property in another component
I have a property in a react component ( Createjob.js ):我在反应组件( 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
and I have a button in another react component and I want it to use this property someting like:我在另一个反应组件中有一个按钮,我希望它使用这个属性,比如:
export class MainInfo extends Component {
<button type='submit' onClick={() => this.Createjob.handleClick('next')} className='next'>ادامه</button>
....
}
how to achieve this?如何做到这一点?
pass the handleClick fucntion as a prop to the another component: as:将handleClick函数作为道具传递给另一个组件:如:
<Component handleClick={this.handleClick} />
then in your button code: this.props.handleClick('next')} className='next'>ادامه然后在您的按钮代码中: this.props.handleClick('next')} className='next'>ادامه
If your button is inside a child component of your Createjob component, you can achieve this by passing the handleClick function as a prop to the child component.如果您的按钮位于 Createjob 组件的子组件内,您可以通过将 handleClick function 作为道具传递给子组件来实现此目的。
const Createjob = () => {.... const handleClick = clickType => { const {currentStep} = this.state let newStep = currentStep clickType === 'next'? newStep++: newStep-- if (newStep > 0 && newStep <= 6) { this.setState({ currentStep: newStep }); } }... return <ChildComponent onClick={handleClick} /> } const ChildComponent = ({onClick}) => <button type='submit' onClick={() => onClick('next')} className='next'>ادامه</button>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.