admin管理员组文章数量:1401133
So I have a DatePicker ChildComponent that I need to access its ref
via forwardRef
from the Parent. Basically everything works fine.
But there's one problem.
Each time I update something on the Parent Other ChildComponents that has no any relation with DatePickerChild, the DatePicker is still rendering even it shouldn't be.
I tried to remove the passing of ref from parent to child then the ponent is not rerendering anymore each time I made an update to other ponents that I want to solve.
<NativeDatePicker
ref={datePickerRef} // <-- When I ment this ponent is not rendering anymore.
currentDate={values.birthday}
onDateChange={handleBirthdayChange}
/>
I already started using callback memoization by using useCallback
to optimize but still no luck.
Here is my code below:
DatePicker.tsx
type Props = {
// ref: RefObject<DatePicker>;
currentDate: string | Date | undefined;
onDateChange: (newDate: string) => void;
};
const NativeDatePicker = forwardRef(
({ currentDate, onDateChange }: Props, ref: any) => {
console.log('tadah!');
return (
<DatePicker
ref={ref}
style={wrapperStyle.wrapper}
date={currentDate}
mode="date"
placeholder="select date"
format="YYYY-MM-DD"
// minDate="2016-05-01"
// maxDate={Date.now()}
confirmBtnText="Confirm"
cancelBtnText="Cancel"
customStyles={styles}
onDateChange={onDateChange}
/>
);
},
);
const MemoizedNativeDatePicker = memo(NativeDatePicker);
I would not show every code in my ParentComponent only the props and refs I'm passing into the DatePicker.
// Memoized Callback Function
const handleBirthdayChange = useCallback(
(newDate: string) => setFieldValue('birthday', newDate),
[setFieldValue],
);
const datePickerRef = createRef<DatePicker>();
const datePickerRefOnPress = () => datePickerRef.current!.onPressDate();
Am I missing something? Thanks for any help.
So I have a DatePicker ChildComponent that I need to access its ref
via forwardRef
from the Parent. Basically everything works fine.
But there's one problem.
Each time I update something on the Parent Other ChildComponents that has no any relation with DatePickerChild, the DatePicker is still rendering even it shouldn't be.
I tried to remove the passing of ref from parent to child then the ponent is not rerendering anymore each time I made an update to other ponents that I want to solve.
<NativeDatePicker
ref={datePickerRef} // <-- When I ment this ponent is not rendering anymore.
currentDate={values.birthday}
onDateChange={handleBirthdayChange}
/>
I already started using callback memoization by using useCallback
to optimize but still no luck.
Here is my code below:
DatePicker.tsx
type Props = {
// ref: RefObject<DatePicker>;
currentDate: string | Date | undefined;
onDateChange: (newDate: string) => void;
};
const NativeDatePicker = forwardRef(
({ currentDate, onDateChange }: Props, ref: any) => {
console.log('tadah!');
return (
<DatePicker
ref={ref}
style={wrapperStyle.wrapper}
date={currentDate}
mode="date"
placeholder="select date"
format="YYYY-MM-DD"
// minDate="2016-05-01"
// maxDate={Date.now()}
confirmBtnText="Confirm"
cancelBtnText="Cancel"
customStyles={styles}
onDateChange={onDateChange}
/>
);
},
);
const MemoizedNativeDatePicker = memo(NativeDatePicker);
I would not show every code in my ParentComponent only the props and refs I'm passing into the DatePicker.
// Memoized Callback Function
const handleBirthdayChange = useCallback(
(newDate: string) => setFieldValue('birthday', newDate),
[setFieldValue],
);
const datePickerRef = createRef<DatePicker>();
const datePickerRefOnPress = () => datePickerRef.current!.onPressDate();
Am I missing something? Thanks for any help.
Share Improve this question edited Jun 15, 2020 at 10:07 joyce asked Jun 15, 2020 at 10:03 joycejoyce 1834 silver badges12 bronze badges1 Answer
Reset to default 5You are creating a ref using createRef
, which on every re-render will return you a new ref instance and hence will lead to failure in optimization of child re-render with React.memo
.
You should instead useuseRef
hook for a functional ponent to create a ref
const datePickerRef = useRef<DatePicker>();
本文标签: javascriptReact using forwardRef always update child component even using memoStack Overflow
版权声明:本文标题:javascript - React using forwardRef always update child component even using memo - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744263733a2597843.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论