Javascript 如何在onClick React中接受1个以上的参数
您好,我是React的初学者,在React组件中有一个按钮,我想将2个选项传递给它的onClick方法,例如:Javascript 如何在onClick React中接受1个以上的参数,javascript,reactjs,jsx,Javascript,Reactjs,Jsx,您好,我是React的初学者,在React组件中有一个按钮,我想将2个选项传递给它的onClick方法,例如: handleClick = clickType => { const {currentStep} = this.state let newStep = currentStep clickType === 'next' ? newStep++ : newStep-- if (newStep > 0 && newStep &l
handleClick = clickType => {
const {currentStep} = this.state
let newStep = currentStep
clickType === 'next' ? newStep++ : newStep--
if (newStep > 0 && newStep <= 6) {
this.setState({
currentStep: newStep
});
}
}
handleChange = input => e => {
this.setState({ [input]: e.target.value });
};
continue = e => {
e.preventDefault();
this.props.nextStep();
};
back = e => {
e.preventDefault();
this.props.prevStep();
};
<button onClick={() => this.handleClick(), this.back} className='previous'>قبلی</button>
<button form='my-form' type='submit' onClick={() => this.handleClick('next'), this.continue} className='next'>ادامه</button>
handleClick=clickType=>{
const{currentStep}=this.state
让newStep=currentStep
单击类型===“下一步”?新闻步骤++:新闻步骤--
如果(newStep>0&&newStep e=>{
this.setState({[input]:e.target.value});
};
继续=e=>{
e、 预防默认值();
this.props.nextStep();
};
后退=e=>{
e、 预防默认值();
this.props.prevStep();
};
this.handleClick(),this.back}className='previous'>>
this.handleClick('next')、this.continue}className='next'>ا
如何才能正确地实现这一点?onClick中的箭头函数可以执行多个函数 它仍然是一个函数,您可以将代码放入其中;) 但也许您可以改进您当前的代码:
handleClick = clickType => {
const {currentStep} = this.state
let newStep = currentStep
clickType === 'next' ? newStep++ : newStep--
if (newStep > 0 && newStep <= 6) {
this.setState({
currentStep: newStep
});
}
}
handleChange = input => e => {
this.setState({ [input]: e.target.value });
};
continue = e => {
e.preventDefault();
this.props.nextStep();
};
back = e => {
e.preventDefault();
this.props.prevStep();
};
<button onClick={(e) => { this.handleClick(); this.back(e); }} className='previous'>قبلی</button>
<button form='my-form' type='submit' onClick={() => { this.handleClick('next'); this.continue(e); }} className='next'>ادامه</button>
handleClick=clickType=>{
const{currentStep}=this.state
让newStep=currentStep
单击类型===“下一步”?新闻步骤++:新闻步骤--
如果(newStep>0&&newStep e=>{
this.setState({[input]:e.target.value});
};
继续=e=>{
e、 预防默认值();
this.props.nextStep();
};
后退=e=>{
e、 预防默认值();
this.props.prevStep();
};
{this.handleClick();this.back(e);}}className='previous'>
{this.handleClick('next');this.continue(e);}className='next'>ا
在此版本中:
handleNext = (e) => {
const { currentStep } = this.state;
if (currentStep >= 0 && currentStep <= 5) {
this.setState({
currentStep: currentStep++
});
}
this.props.nextStep();
}
handlePrevious = (e) => {
const { currentStep } = this.state;
if (currentStep > 0 && currentStep <= 5) {
this.setState({
currentStep: currentStep--
});
}
this.props.prevStep();
}
<button onClick={this.handlePrevious} className='previous'>قبلی</button>
<button form='my-form' type='submit' onClick={this.handleNext} className='next'>ادامه</button>
handleNext=(e)=>{
const{currentStep}=this.state;
如果(currentStep>=0&¤tStep{
const{currentStep}=this.state;
如果(currentStep>0&¤tStep单击onClick
中的箭头函数可以执行多个函数
它仍然是一个函数,您可以将代码放入其中;)
但也许您可以改进您当前的代码:
handleClick = clickType => {
const {currentStep} = this.state
let newStep = currentStep
clickType === 'next' ? newStep++ : newStep--
if (newStep > 0 && newStep <= 6) {
this.setState({
currentStep: newStep
});
}
}
handleChange = input => e => {
this.setState({ [input]: e.target.value });
};
continue = e => {
e.preventDefault();
this.props.nextStep();
};
back = e => {
e.preventDefault();
this.props.prevStep();
};
<button onClick={(e) => { this.handleClick(); this.back(e); }} className='previous'>قبلی</button>
<button form='my-form' type='submit' onClick={() => { this.handleClick('next'); this.continue(e); }} className='next'>ادامه</button>
handleClick=clickType=>{
const{currentStep}=this.state
让newStep=currentStep
单击类型===“下一步”?新闻步骤++:新闻步骤--
如果(newStep>0&&newStep e=>{
this.setState({[input]:e.target.value});
};
继续=e=>{
e、 预防默认值();
this.props.nextStep();
};
后退=e=>{
e、 预防默认值();
this.props.prevStep();
};
{this.handleClick();this.back(e);}}className='previous'>
{this.handleClick('next');this.continue(e);}className='next'>ا
在此版本中:
handleNext = (e) => {
const { currentStep } = this.state;
if (currentStep >= 0 && currentStep <= 5) {
this.setState({
currentStep: currentStep++
});
}
this.props.nextStep();
}
handlePrevious = (e) => {
const { currentStep } = this.state;
if (currentStep > 0 && currentStep <= 5) {
this.setState({
currentStep: currentStep--
});
}
this.props.prevStep();
}
<button onClick={this.handlePrevious} className='previous'>قبلی</button>
<button form='my-form' type='submit' onClick={this.handleNext} className='next'>ادامه</button>
handleNext=(e)=>{
const{currentStep}=this.state;
如果(currentStep>=0&¤tStep{
const{currentStep}=this.state;
如果(currentStep>0&¤tStep您需要以不同的方式设置处理功能
而是有这样的东西:
handleBack = e => {
e.preventDefault()
if (this.state.currentStep > 1) {
this.setState((prevState) => ({
currentStep: prevState.currentStep - 1
}));
}
this.props.prevStep()
}
handleNext = e => {
e.preventDefault()
if (this.state.currentStep < 6) {
this.setState((prevState) => ({
currentStep: prevState.currentStep + 1
}));
}
this.props.nextStep()
}
<button onClick={this.handleBack} ... />
<button onClick={this.handleNext} ... />
handleBack=e=>{
e、 预防默认值()
如果(this.state.currentStep>1){
this.setState((prevState)=>({
currentStep:prevState.currentStep-1
}));
}
this.props.prevStep()
}
handleNext=e=>{
e、 预防默认值()
if(this.state.currentStep<6){
this.setState((prevState)=>({
currentStep:prevState.currentStep+1
}));
}
this.props.nextStep()
}
此方法更简洁,更易于阅读,因为每个函数都处理自己的单击
现在,单击“上一步”和“下一步”时,您可以清楚地看到正在发生的事情。您需要以不同的方式设置处理功能
而是有这样的东西:
handleBack = e => {
e.preventDefault()
if (this.state.currentStep > 1) {
this.setState((prevState) => ({
currentStep: prevState.currentStep - 1
}));
}
this.props.prevStep()
}
handleNext = e => {
e.preventDefault()
if (this.state.currentStep < 6) {
this.setState((prevState) => ({
currentStep: prevState.currentStep + 1
}));
}
this.props.nextStep()
}
<button onClick={this.handleBack} ... />
<button onClick={this.handleNext} ... />
handleBack=e=>{
e、 预防默认值()
如果(this.state.currentStep>1){
this.setState((prevState)=>({
currentStep:prevState.currentStep-1
}));
}
this.props.prevStep()
}
handleNext=e=>{
e、 预防默认值()
if(this.state.currentStep<6){
this.setState((prevState)=>({
currentStep:prevState.currentStep+1
}));
}
this.props.nextStep()
}
此方法更简洁,更易于阅读,因为每个函数都处理自己的单击
现在,当您单击“上一步”时,您可以清楚地看到正在发生的事情,当您单击“下一步”时,也可以清楚地看到正在发生的事情。您可以使用类似的方法
/**
I copied this function from code, please make sure that its working.
*/
handleChange = input => e => {
this.setState({ [input]: e.target.value });
};
updateStep = step => {
if (step > 0 && step <= 6)
this.setState({
currentStep: newStep
});
}
/**
Try to avoid the keywords like continue, break, for, while etc as
variable or function names.
*/
handleContinue = e => {
e.preventDefault();
this.handleClick(this.state.currentStep+1);
this.props.nextStep();
};
handleBack = e => {
e.preventDefault();
this.handleClick(this.state.currentStep-1);
this.props.prevStep();
};
<button onClick={this.handleBack} className='previous'>قبلی</button>
<button form='my-form' type='submit' onClick={this.handleContinue} className='next'>ادامه</button>
/**
我从代码中复制了这个函数,请确保它正常工作。
*/
handleChange=input=>e=>{
this.setState({[input]:e.target.value});
};
updateStep=step=>{
如果(步骤>0&&步骤{
e、 预防默认值();
this.handleClick(this.state.currentStep+1);
this.props.nextStep();
};
车把靠背=e=>{
e、 预防默认值();
this.handleClick(this.state.currentStep-1);
this.props.prevStep();
};
قبلی
ادامه
您可以使用类似的方法
/**
I copied this function from code, please make sure that its working.
*/
handleChange = input => e => {
this.setState({ [input]: e.target.value });
};
updateStep = step => {
if (step > 0 && step <= 6)
this.setState({
currentStep: newStep
});
}
/**
Try to avoid the keywords like continue, break, for, while etc as
variable or function names.
*/
handleContinue = e => {
e.preventDefault();
this.handleClick(this.state.currentStep+1);
this.props.nextStep();
};
handleBack = e => {
e.preventDefault();
this.handleClick(this.state.currentStep-1);
this.props.prevStep();
};
<button onClick={this.handleBack} className='previous'>قبلی</button>
<button form='my-form' type='submit' onClick={this.handleContinue} className='next'>ادامه</button>
/**
我从代码中复制了这个函数,请确保它正常工作。
*/
handleChange=input=>e=>{
this.setState({[input]:e.target.value});
};
updateStep=step=>{
如果(步骤>0&&步骤{
e、 预防默认值();
this.handleClick(this.state.currentStep+1);
this.props.nextStep();
};
车把靠背=e=>{
e、 预防默认值();
this.handleClick(this.state.currentStep-1);
this.props.prevStep();
};
قبلی
ادامه
试试这个onClick={e=>This.handleClick(e,This.back)}onClick={(e)=>{This.handleClick();This.back(e)}
试试这个onClick={e=>This.handleClick(e,This.back)}onClick={(e)=>{This.handleClick();This.back(e)}