react-use#useUpdateEffect TypeScript Examples

The following examples show how to use react-use#useUpdateEffect. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: input-select.tsx    From erda-ui with GNU Affero General Public License v3.0 6 votes vote down vote up
CP_INPUT_SELECT = (props: CP_INPUT_SELECT.Props) => {
  const { props: configProps, state: propsState, operations, execOperation } = props;
  const { options, visible = true, ...rest } = configProps || {};

  const [state, updater, update] = useUpdate({
    value: propsState.value || (undefined as string | undefined),
  });

  useUpdateEffect(() => {
    update({
      value: propsState.value || undefined,
    });
  }, [propsState]);

  useUpdateEffect(() => {
    operations?.onChange && execOperation(operations.onChange, { value: state.value });
  }, [state.value]);

  const loadData = (selectedOptions: any) => {
    operations?.onSelectOption && execOperation(operations?.onSelectOption, selectedOptions);
  };

  const onChange = (v: string) => {
    updater.value(v);
  };

  if (!visible) return null;
  return <InputSelect {...rest} options={options} onChange={onChange} value={state.value} onLoadData={loadData} />;
}
Example #2
Source File: QuantityInput.tsx    From storefront with MIT License 6 votes vote down vote up
QuantityInput: React.VFC<QuantityInputProps> = ({
  disabled,
  max = Infinity,
  min = 0,
  onChange,
  value: initialValue,
}) => {
  const [value, { inc, dec }] = useCounter(initialValue ?? 0, max, min);

  useUpdateEffect(() => {
    onChange(value);
  }, [value]);

  return (
    <Box sx={{ display: 'inline-flex', alignItems: 'center' }}>
      <IconButton
        aria-label="Minus"
        // size="small"
        disabled={disabled || value === min}
        onClick={() => dec()}
      >
        <Minus />
      </IconButton>
      <Box sx={{ mx: 1 }}>
        <Typography variant="body2">{value}</Typography>
      </Box>
      <IconButton
        aria-label="Plus"
        // size="small"
        disabled={disabled || value === max}
        onClick={() => inc()}
      >
        <Plus />
      </IconButton>
    </Box>
  );
}
Example #3
Source File: use-hooks.tsx    From erda-ui with GNU Affero General Public License v3.0 6 votes vote down vote up
useUpdateSearch = (props?: IUseUpdateSearchProps): [setQuery: (q: Obj) => void, query: Obj] => {
  const { convertQuery = defaultConvert, reload } = props || {};
  const [routeQuery, urlState] = routeInfoStore.useStore((s) => [s.query, s.urlState]);
  const [query, setQuery] = React.useState(convertQuery(routeQuery));

  const urlStateRef = React.useRef(urlState);
  const [isReload, setIsReload] = React.useState(false);
  const reloadRef = React.useRef(isReload);

  React.useImperativeHandle(reloadRef, () => isReload);
  React.useImperativeHandle(urlStateRef, () => urlState);

  useUpdateEffect(() => {
    if (urlState === 'back' || urlState === 'forward') {
      const curUrlQuery = convertQuery(routeQuery);
      setQuery(curUrlQuery);
      setIsReload(true);
      reload?.(curUrlQuery);
    }
  }, [urlState, routeQuery]);

  useUpdateEffect(() => {
    if (!isReload) {
      updateSearch({ ...query });
    }
  }, [query]);

  const _setQuery = React.useCallback((q: Obj) => {
    if (reloadRef.current) {
      setIsReload(false);
    } else {
      setQuery(q);
    }
  }, []);

  return [_setQuery, query];
}