[英]React: delay in input changes
I have a rather big array of objects in the parent component's state.我在父组件的状态中有一个相当大的对象数组。 And when I pass them as props to children components (which don't have state, only props) I have a huge delay in typing (in children's components) when the input changes.当我将它们作为道具传递给子组件(没有状态,只有道具)时,我在输入更改时(在子组件中)打字会有很大的延迟。
I have already read answers on similar questions ReactJS delay onChange while typing and advice to use shouldcomponentupdate reactjs : ShouldComponentUpdate for states .我已经阅读了类似问题的答案ReactJS 在键入时延迟 onChange和建议使用 shouldcomponentupdate reactjs: ShouldComponentUpdate for states 。
But, unfortunately, I still didn't understand how to apply it in my example : https://codesandbox.io/s/react-example-8vlc2 .但是,不幸的是,我仍然不明白如何在我的示例中应用它: https : //codesandbox.io/s/react-example-8vlc2 。 Parent component is index.js .父组件是 index.js 。 So:所以:
1) Should I use componentdidupdate or shouldcomponentupdate() in children's components (StoryList.tsx and StoriesScreenItem/StoriesScreenList) ? 1) 我应该在儿童组件(StoryList.tsx 和 StoriesScreenItem/StoriesScreenList)中使用 componentdidupdate 还是 shouldcomponentupdate() ?
2) Should I add state in children's component to use componentdidupdate or shouldcomponentupdate() ? 2)我应该在子组件中添加状态以使用 componentdidupdate 还是 shouldcomponentupdate() ?
3) Why does the input delay happen in my example? 3)为什么在我的例子中会发生输入延迟?
4) Or any other ideas how can I manage this problem? 4)或任何其他想法如何解决这个问题?
Any help would be appreciated!任何帮助,将不胜感激!
My suggestion would be to create a functional component for a story.我的建议是为故事创建一个功能组件。
It could look something like this:它可能看起来像这样:
export const Story = props => {
const [story, setStory] = useState(props.story);
const handleChange = e => {
const updatedStory = {...story};
updatedStory.caption = e.target.value;
setStory(updatedStory);
};
return (
<div>
<div>
<div>
{story.previewUri ? (
<div>
<img
style={{
objectFit: "cover",
border: "4px solid #1ec1b6"
}}
width="110"
height="168"
src={story.previewUri || null}
alt={"Фото сториc"}
/>
</div>
) : (
<div
style={{
border: "4px solid #1ec1b6",
textAlign: "center",
fontSize: "14px"
}}
>
Link image
</div>
)}
<div>
<span>Story</span>
<div>
<div>
<span>Preview title</span>
</div>
<div>
<input
style={{ width: "100%" }}
maxLength={99}
value={story.caption}
onChange={e => handleChange(e)}
/>
</div>
</div>
</div>
</div>
<StoriesScreenList
screens={story.stories}
onChange={screens => props.changeScreen(screens, props.index)}
/>
</div>
</div>
);
};
export default Story;
Each of these would have their own state, so you would not have to update the entire array of stories each time a single story is changed.其中每一个都有自己的状态,因此您不必在每次更改单个故事时更新整个故事数组。
Which is suspect is what creates the delay.可疑的是造成延迟的原因。
I Hope this is helpful.我希望这是有帮助的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.