Reactjs onChange在哪里向其处理者传递参数
用下面的代码Reactjs onChange在哪里向其处理者传递参数,reactjs,components,onchange,Reactjs,Components,Onchange,用下面的代码 <div className="input-first-line-right"> <Input type="textarea" label="Project Description" onChange={this.handleDescChange} value={this.state.projectDescription} /> </div> 如果HandleDeschange需要一个参数“text”,那么它为什么永远不
<div className="input-first-line-right">
<Input
type="textarea"
label="Project Description"
onChange={this.handleDescChange}
value={this.state.projectDescription}
/>
</div>
如果HandleDeschange需要一个参数“text”,那么它为什么永远不会被传递。
意思是为什么代码不是
onChange={this.handleDescChange("some new text")}
为了使函数工作。如果没有任何东西传递给代码,代码如何从本质上知道参数是什么。对于
onChange
属性,this.handleDescChange
在这里不被调用。
这里,this.handleDescChange
作为回调提供。当触发更改事件时,输入组件调用this.handleDescChange
如果要传递变量,可以使用fat arrow函数。下面给出了解决方案
<div className="input-first-line-right">
<Input
type="textarea"
label="Project Description"
onChange={event => this.handleDescChange(event.target.value)}
value={this.state.projectDescription}
/>
</div>
对于
onChange
属性,这里不调用this.handleDescChange
。
这里,this.handleDescChange
作为回调提供。当触发更改事件时,输入组件调用this.handleDescChange
如果要传递变量,可以使用fat arrow函数。下面给出了解决方案
<div className="input-first-line-right">
<Input
type="textarea"
label="Project Description"
onChange={event => this.handleDescChange(event.target.value)}
value={this.state.projectDescription}
/>
</div>
当我们尝试以异步方式访问React合成事件时,会触发此警告。由于重用,在调用事件回调后,合成事件对象将不再存在,因此我们无法访问其属性
上面的源链接有正确答案,但以下是摘要:
类应用程序扩展组件{
建造师(道具){
超级(道具);
此.state={
项目描述:“
};
}
HandleDeschange=事件=>{
//加在这里
event.persist();
//不检查“!text”检查是否有值
如果(event.target.value==0)返回;
this.setState({projectDescription:event.target.value});
};
render(){
返回(
);
}
}
尽管如此,我还是建议开始学习/使用React,因为这是一种更干净、优雅的方法:
import React, { useState } from "react";
const App = () => {
// useState hook
const [projectDescription, setProjectDescription] = useState("");
const handleDescChange = event => {
if (event.target.value === 0) return;
setProjectDescription(event.target.value);
};
return (
<div className="input-first-line-right">
<input
type="textarea"
label="Project Description"
onChange={handleDescChange}
value={projectDescription}
/>
</div>
);
};
import React,{useState}来自“React”;
常量应用=()=>{
//使用状态挂钩
const[projectDescription,setProjectDescription]=useState(“”);
const handleDescChange=事件=>{
如果(event.target.value==0)返回;
setProjectDescription(event.target.value);
};
返回(
);
};
当我们尝试以异步方式访问React合成事件时,会触发此警告。由于重用,在调用事件回调后,合成事件对象将不再存在,因此我们无法访问其属性
上面的源链接有正确答案,但以下是摘要:
类应用程序扩展组件{
建造师(道具){
超级(道具);
此.state={
项目描述:“
};
}
HandleDeschange=事件=>{
//加在这里
event.persist();
//不检查“!text”检查是否有值
如果(event.target.value==0)返回;
this.setState({projectDescription:event.target.value});
};
render(){
返回(
);
}
}
尽管如此,我还是建议开始学习/使用React,因为这是一种更干净、优雅的方法:
import React, { useState } from "react";
const App = () => {
// useState hook
const [projectDescription, setProjectDescription] = useState("");
const handleDescChange = event => {
if (event.target.value === 0) return;
setProjectDescription(event.target.value);
};
return (
<div className="input-first-line-right">
<input
type="textarea"
label="Project Description"
onChange={handleDescChange}
value={projectDescription}
/>
</div>
);
};
import React,{useState}来自“React”;
常量应用=()=>{
//使用状态挂钩
const[projectDescription,setProjectDescription]=useState(“”);
const handleDescChange=事件=>{
如果(event.target.value==0)返回;
setProjectDescription(event.target.value);
};
返回(
);
};
import React, { useState } from "react";
const App = () => {
// useState hook
const [projectDescription, setProjectDescription] = useState("");
const handleDescChange = event => {
if (event.target.value === 0) return;
setProjectDescription(event.target.value);
};
return (
<div className="input-first-line-right">
<input
type="textarea"
label="Project Description"
onChange={handleDescChange}
value={projectDescription}
/>
</div>
);
};