不能用作JSX组件。它的返回类型' promist< element>'不是有效的JSX元素

发布于 2025-01-27 22:59:50 字数 1144 浏览 4 评论 0原文

我有一个React组件 编辑器。我正在尝试使用async函数初始化 状态。但是我无法。

我们如何在react中做到这一点。

const Editor = () => {
  const { id } = useParams();
  const [schemas, updateSchemas] = useAtom(bfsAtom);
  const schema = id && _.get(schemas, id, {});

  type InitialStateType = {
    properties: KeyedProperty[];
    validations: ValidationDataProperty[];
  };
  const getInitialState = async (): Promise<InitialStateType> => {
    return {
      properties: createPropertiesFromSchema(schema),
      validations: initializeConditions(schema),
    };
  };

  const initialState = await getInitialState();

  const mainReducer = (
    { properties, validations }: InitialStateType,
    action: Action
  ) => ({
    properties: propertyReducer(properties, action),
    validations: validationReducer(validations, action),
  });
  const [state, dispatch] = useReducer(mainReducer, initialState);

  return (
    <PropertyContext.Provider value={{ state, dispatch }}>
      <SchemaEditor schema={schema}  />
    </PropertyContext.Provider>
  );
};

I have an React Component Editor. I am trying to initialize the state using an async function. But I am unable to .

How we can do that in React.

const Editor = () => {
  const { id } = useParams();
  const [schemas, updateSchemas] = useAtom(bfsAtom);
  const schema = id && _.get(schemas, id, {});

  type InitialStateType = {
    properties: KeyedProperty[];
    validations: ValidationDataProperty[];
  };
  const getInitialState = async (): Promise<InitialStateType> => {
    return {
      properties: createPropertiesFromSchema(schema),
      validations: initializeConditions(schema),
    };
  };

  const initialState = await getInitialState();

  const mainReducer = (
    { properties, validations }: InitialStateType,
    action: Action
  ) => ({
    properties: propertyReducer(properties, action),
    validations: validationReducer(validations, action),
  });
  const [state, dispatch] = useReducer(mainReducer, initialState);

  return (
    <PropertyContext.Provider value={{ state, dispatch }}>
      <SchemaEditor schema={schema}  />
    </PropertyContext.Provider>
  );
};

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

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

发布评论

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

评论(1

素食主义者 2025-02-03 22:59:50

tl; dr async从顶层到内部的内部dok dot dot the Tracs


不是直接与您的问题联系在一起,而是我做错了的一件事是:

我有一个看起来像:

const MyComponent = async () => {
   ...
   const apiResponse = await apiFunction();

   return <div>
     
     {apiResponse.success && (
       <p>It was a success!</p>
     )}

   </div>
}

的组件,您不应该在最高级别的组件中使用async。因此,要修复它我做到了:

const MyComponent = () => {
   ...
   const apiResponse = async () => {
     return await apiFunction();
   }

   return <div>
     
     {apiResponse().success && (
       <p>It was a success!</p>
     )}

   </div>
}

TL;DR moving the async from top level to inside the component did the trick


Is not directly linked with your question but one thing that I was doing wrong is the following:

I had a component that looked like:

const MyComponent = async () => {
   ...
   const apiResponse = await apiFunction();

   return <div>
     
     {apiResponse.success && (
       <p>It was a success!</p>
     )}

   </div>
}

BUT you shouldn't use async in your components at top level. So to fix it I did this:

const MyComponent = () => {
   ...
   const apiResponse = async () => {
     return await apiFunction();
   }

   return <div>
     
     {apiResponse().success && (
       <p>It was a success!</p>
     )}

   </div>
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文