Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

selector seems not get new value in sometimes #2287

Open
ZHUBFeng opened this issue Sep 12, 2023 · 1 comment
Open

selector seems not get new value in sometimes #2287

ZHUBFeng opened this issue Sep 12, 2023 · 1 comment

Comments

@ZHUBFeng
Copy link

ZHUBFeng commented Sep 12, 2023

This is a problem that can not be repeated consistently,I don't know if it's the code or the recoil

Here's the thing

under normal circumstances
image

under abnormal circumstances,sees left menu is not display,the second "GlobalMenu suspendVisible" is not console
image

export const menuConfigAtom = atom<{
  fixed?: boolean;
  suspendVisible?: boolean;
  extendVisible?: boolean;
}>({
  key: 'ocean_menu_config',
  default: { fixed: localStorage.getItem(`${OCEAN_PREFIX}_menu_fixed`) === 'true' },
});

/** 菜单相关展示配置 */
export const menuConfigSelector = selector({
  key: 'ocean_menu_config_selector',
  get: ({ get }) => get(menuConfigAtom),1
  set: ({ set }, newValue) => {
    if (newValue instanceof DefaultValue) {
      return;
    }
    set(menuConfigAtom, newValue);

    if (!newValue.extendVisible) {
      set(navSearchKeywordAtom, '');
      set(navSortAtom, false);
    }
  },
});

export function useMenuVisible() {
  const [iconRef, isIconHovered] = useHover<HTMLDivElement>();
  const [menuRef, isMenuHovered] = useHover<HTMLDivElement>();
  const setMenuConfig = useSetRecoilState(menuConfigSelector);
  const navType = useNavType();

  const { run } = **useDebounceFn**(visible => {
    setMenuConfig(pre => {
      console.log('----', pre, visible);

      return {
        ...pre,
        suspendVisible: visible,
        ...(!visible ? { extendVisible: visible } : {}),
      };
    });
  }, 100);

  useEffect(() => {
    if (isIconHovered || isMenuHovered) {
      run(true);
    }

    if (!(isIconHovered || isMenuHovered)) {
      run(false);
    }
  }, [isIconHovered, isMenuHovered, navType]);

  return { iconRef, menuRef };
}

the menu button on the top and the control of the left menu display are both suspendedVisible variables, and they both use useRecoilState to retrieve data

Did I guess it right that it was caused by the left menu component not being triggered?,by the way, I used debounce to control the change of "menuconfig" data. Is this related to it? and i use "React.forwardRef", Is this related to it?

@ZHUBFeng
Copy link
Author

ZHUBFeng commented Sep 12, 2023

There is also an issue with using a selector elsewhere, where after changing the data, using useRecoilValue(xxxSelector) returns undefined, while using the atom directly retrieves the latest value and triggers component rendering.

Sometimes useRecoilValue(currentAppSelector) fails to retrieve the value, while using useRecoilValue(currentAppAtom) seems to work fine. As this is an intermittent issue, it cannot be confirmed entirely if it is normal.

export const currentAppAtom = atom<string>({
  key: 'ocean_current_app',
  default: '',
});

export const currentAppSelector = selector({
  key: 'ocean_current_app_selector',
  get: ({ get }) => get(currentAppAtom),
  set: ({ set }, name) => {
    if (name instanceof DefaultValue) {
      return;
    }

    set(currentAppAtom, appKey);
  },
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant