reactjs - 反应 - 嵌套箭头组件的文本区域 - 无法在值更改时调用设置状态
问题描述
我想调用父组件来设置状态。
在内部组件中输入输入时,它会失去焦点。
例子:
const X = () => {
const Y = (props) => <textarea onChange={(e)=>{props._s(e.target.value)}}/>
const [comments, setComments] = useState("");
return (
<div>
<Y _s={setComments}/>
</div>
)
}
在组件中声明箭头函数组件有问题吗?
问候
解决方案
尝试在组件之外声明 Y,这样它就不会在每次渲染时重新创建
const Y = (props) => <textarea onChange={(e)=>{props._s(e.target.value)}}/>
const X = () => {
const [comments, setComments] = useState("");
return (
<div>
<Y _s={setComments}/>
</div>
)
}
推荐阅读
- docker - 用最新标签标记的 docker 图像未检测到 docker 存储库中的最新图像刷新
- javascript - 如何使用来自另一个数组的匹配对象过滤数组并将其推送到新数组?
- python - 如何识别和区分类名、对象属性和变量、对象方法?
- python - Python 9.8 LAB:艺术品标签(使用构造函数定义类)
- flutter - Flutter 自动生成 json/arb 文件的密钥到代码
- javascript - React 中的绑定与箭头函数性能
- math - 如何在 4D 平面上找到一个点周围均匀分布的点?
- ios - WkWebView Cordova Ios onClick 事件未触发
- javascript - Antd提示框悬停后如何消失?
- java - 给定查询参数的具有空值和非空值的 JPA 查询