admin管理员组文章数量:1326104
I have parent ponent with multiple child ponents. Different types of data will be passed to child ponents after getting data from api. Some of the ponents will get object as a prop. I am trying to avoid rerenders in that ponent. Even if the data is same it is rerendering. How can i avoid this rerenders?
const Parent = () => {
const [childData, setChildData] = useState(null);
useEffect(() => {
const data = getChildData();
setChildData(data);
}, [])
return (
<Child data={childData}/>
);
};
const Child = React.memo((props) => {
const {name, email} = props.data;
return (
<div>
<p>Name: {name}</p>
<p>Email: {email}</p>
</div>
);
});
I have parent ponent with multiple child ponents. Different types of data will be passed to child ponents after getting data from api. Some of the ponents will get object as a prop. I am trying to avoid rerenders in that ponent. Even if the data is same it is rerendering. How can i avoid this rerenders?
const Parent = () => {
const [childData, setChildData] = useState(null);
useEffect(() => {
const data = getChildData();
setChildData(data);
}, [])
return (
<Child data={childData}/>
);
};
const Child = React.memo((props) => {
const {name, email} = props.data;
return (
<div>
<p>Name: {name}</p>
<p>Email: {email}</p>
</div>
);
});
Share
Improve this question
asked Sep 8, 2020 at 13:17
Sudeep KumarSudeep Kumar
9891 gold badge7 silver badges16 bronze badges
2
- 2 You should take a look at memoization : reactjs/docs/react-api.html#reactmemo – Florian Motteau Commented Sep 8, 2020 at 13:20
- Here's a similar question stackoverflow./questions/57705867/… – Abdul Azeem Commented Sep 9, 2020 at 8:05
2 Answers
Reset to default 7As per React Official Docs, By default React will only shallowly pare plex objects in the props object. If you want control over the parison, you can also provide a custom parison function as the second argument. https://reactjs/docs/react-api.html#reactmemo. Here is an example.
const Parent = () => {
const [childData, setChildData] = useState(null);
useEffect(() => {
const data = getChildData();
setChildData(data);
}, [])
return (
<Child data={childData} />
);
};
function areEqual(prevProps, nextProps) {
return prevProps.name == nextProps.name && prevProps.email == nextProps.email;
}
const Child = React.memo((props) => {
const { name, email } = props.data;
return (
<div>
<p>Name: {name}</p>
<p>Email: {email}</p>
</div>
);
}, areEqual);
By default React.memo will only shallowly pare
plex objects in the props
.
You can use custom equality check of props. Pass the custom check function as second argument to React.memo
similar to below
const areEqual = (prevProps, nextProps) => {
const {name, email} = prevProps.data;
const {name: nextName, email: nextEmail} = nextProps.data;
return name === nextName && email === nextEmail;
};
const Child = React.memo((props) => {...}, areEqual);
For more details check here.
本文标签: javascriptHow to avoid rerendering when i pass an object as prop to child componentStack Overflow
版权声明:本文标题:javascript - How to avoid re-rendering when i pass an object as prop to child component? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742192741a2430517.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论