我有这个代码。
这是代码片段
const [indicators, setIndicators] = useState([]);
const [curText, setCurText] = useState('');
const refIndicator = useRef()
useEffect(() => {
console.log(indicators)
}, [indicators]);
const onSubmit = (e) => {
e.preventDefault();
setIndicators([...indicators, curText]);
setCurText('')
}
const onChange = (e) => {
setCurText(e.target.value);
}
const MemoInput = memo((props)=>{
console.log(props)
return(
<ShowIndicator name={props.name}/>
)
},(prev, next) => console.log('prev',prev, next)
);
每次我在表单中添加时,它都会显示每个指标。
问题是ShowIndicator
每次我添加东西时都会更新。
有没有办法让我限制我的应用程序渲染的时间,因为例如我创建了 3 ShowIndicators
,那么它也会渲染 3 次,我认为从长远来看这是非常昂贵的。
我也在考虑使用 useRef 只是为了不让我的应用程序在每次输入新文本时都呈现,但我不确定它是否是正确的实现,因为大多数文档都建议通过使用状态作为当前值的处理程序来使用受控组件。