简体   繁体   English

使用 forwardRef 反应总是更新子组件,即使使用备忘录

[英]React using forwardRef always update child component even using memo

So I have a DatePicker ChildComponent that I need to access its ref via forwardRef from the Parent .所以我有一个 DatePicker ChildComponent ,我需要通过ParentforwardRef访问它的ref 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.每次我在 Parent Other ChildComponents上更新与DatePickerChild没有任何关系的内容时, DatePicker 仍在呈现,即使它不应该呈现。

I tried to remove the passing of ref from parent to child then the component is not rerendering anymore each time I made an update to other components that I want to solve.我试图删除 ref 从父级到子级的传递,然后每次我对要解决的其他组件进行更新时,该组件都不再重新渲染。

<NativeDatePicker
  ref={datePickerRef} // <-- When I comment this component is not rendering anymore.
  currentDate={values.birthday}
  onDateChange={handleBirthdayChange}
/>

I already started using callback memoization by using useCallback to optimize but still no luck.我已经开始通过使用useCallback来优化回调记忆,但仍然没有运气。

Here is my code below:下面是我的代码:

DatePicker.tsx日期选择器.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.我不会只显示我的ParentComponent中的每个代码我传递给 DatePicker 的 props 和 refs。

 // 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.谢谢你的帮助。

You 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 .您正在使用createRef创建一个 ref,它在每次重新渲染时都会返回一个新的 ref 实例,因此会导致使用React.memo优化子重新渲染失败。

You should instead use useRef hook for a functional component to create a ref您应该改为使用useRef钩子作为功能组件来创建 ref

const datePickerRef = useRef<DatePicker>();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 React 的 forwardRef 内联或作为组件 - Using React's forwardRef inline or as a component React 父组件道具无法在子组件中使用 forwardRef 道具访问 - React parent component props can't access using forwardRef props in child component 使用 React.forwardRef() 在 React 中调用子组件函数的正确方法是什么? - What is the right way to call child component functions in React using React.forwardRef()? React 备忘录子组件渲染 - React memo child component render 即使使用 React.memo,更改孩子的道具是否总是重新渲染父母? - Does changing the props of a child always re-render the parent even with React.memo? 防止使用 React 和 React-memo 重新渲染组件 - prevent re render component using React and React-memo 在其他组件中使用 forwardRef 出现类型错误 - Using forwardRef in other component got type error 当它们在同一页面中时,如何使用 Hook(仅限 ForwardRef 概念)在子组件中调用父方法 - How to call a Parent method in Child Component using Hook (ForwardRef concept only) when they are in same page 使用 getDerivedStateFromProps 从父级反应更新子组件 state - React update child component state from parent using getDerivedStateFromProps 使用 React 路由器路由请求以更新部分子组件 - Routing requests to update part of child component using React router
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM