将 props 传递给组件包装器

发布于 2025-01-10 16:54:50 字数 1325 浏览 3 评论 0原文

我有一个在两个子组件之间切换的高阶组件(呈现数据和加载组件的组件): ,问题是如何将数据组件中发生的加载状态传递给临时组件,以便进行条件渲染

Wrapper:

function withLoading<Props>(DataComponent: React.FC<Props>, LoadingComponent: React.FC<Partial<Props>>) {
  const newComponent: React.FC<Props & { isLoading: boolean }> = (props) => {
    const {
      isLoading,
    } = props;
    if (isLoading) {
      return (
        <LoadingComponent
          {...props}
        />
      );
    }
    return (
      <DataComponent
        {...props}
      />
    );
  };
  return newComponent;
}

数据和加载组件:

const DataComponent: React.FC<Props> = (props: Props) => {

  const fetchData = async (): Promise<void> => {
    //Loading Logic happens in this mobx store
    await getDataFromBackend();
  };

  useEffect(() => {
    getBranchName();
  }, []);

return (
  //UI
)

const LoadingComponent = () => {
  const {
    selectStyle,
  } = useStyles(styles);
  return (
    <SkeletonPlaceholderWrapper>
      <View style={styles.loader} />
    </SkeletonPlaceholderWrapper>
  );
};

export default observer(
    withLoading(observer(DataComponent), LoadingComponent) //here the hoc wrapping the two children
  ),

I have a higher order component that switches between two children (component that renders data and loading component):
,the issue is how to pass the loading state that happens in the data component to the hoc in order to do the conditional rendering

Wrapper:

function withLoading<Props>(DataComponent: React.FC<Props>, LoadingComponent: React.FC<Partial<Props>>) {
  const newComponent: React.FC<Props & { isLoading: boolean }> = (props) => {
    const {
      isLoading,
    } = props;
    if (isLoading) {
      return (
        <LoadingComponent
          {...props}
        />
      );
    }
    return (
      <DataComponent
        {...props}
      />
    );
  };
  return newComponent;
}

Data and loading components:

const DataComponent: React.FC<Props> = (props: Props) => {

  const fetchData = async (): Promise<void> => {
    //Loading Logic happens in this mobx store
    await getDataFromBackend();
  };

  useEffect(() => {
    getBranchName();
  }, []);

return (
  //UI
)

const LoadingComponent = () => {
  const {
    selectStyle,
  } = useStyles(styles);
  return (
    <SkeletonPlaceholderWrapper>
      <View style={styles.loader} />
    </SkeletonPlaceholderWrapper>
  );
};

export default observer(
    withLoading(observer(DataComponent), LoadingComponent) //here the hoc wrapping the two children
  ),

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

猫七 2025-01-17 16:54:50

在包装器中更改以下内容:

const [loading, setLoading] = useState(isLoading)

if(isLoading) 更改为 if(loading)

将函数 setLoading 作为 props 传递给 DataComponent

<DataComponent onComplete = {() => setLoading(false)} />

在 DataComponent 中,更新您的 fetchData< /代码> 方法:

const fetchData = async (): Promise<void> => {
    //Loading Logic happens in this mobx store
    await getDataFromBackend();
    props.onComplete() //<--- add this line
};

In the wrapper change this :

const [loading, setLoading] = useState(isLoading)

Change the if(isLoading) to if(loading)

Pass the function setLoading as props to DataComponent

<DataComponent onComplete = {() => setLoading(false)} />

In the DataComponent, update your fetchData method :

const fetchData = async (): Promise<void> => {
    //Loading Logic happens in this mobx store
    await getDataFromBackend();
    props.onComplete() //<--- add this line
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文