简体   繁体   English

失败的道具类型:提供给`GlobalState`的`array`类型的无效道具`children`

[英]Failed prop type: Invalid prop `children` of type `array` supplied to `GlobalState`

I am having error like this in my page:我的页面中有这样的错误:

> Warning: Failed prop type: Invalid prop `children` of type `array`
> supplied to `GlobalState`, expected `object`.
>     in GlobalState (created by MyApp)
>     in MyApp
>     in ErrorBoundary (created by ReactDevOverlay)
>     in ReactDevOverlay (created by Container)
>     in Container (created by AppContainer)
>     in AppContainer
>     in Root

Here is following code when I render MyApp :这是我渲染MyApp时的以下代码:

  render() {
    const { Component, pageProps } = this.props;

    return (
      <>
        {/* <CssBaseline /> */}
        <Meta />
        <ToastContainer />
        <ThemeProvider theme={theme}>
          <GlobalState {...pageProps}>
            <ProgressBar />
            <MessageContextProvider>
              {!isEmpty(pageProps?.serverUserData) && (
                <FloatingChat currentUser={{ ...pageProps?.serverUserData }} />
              )}
              <Component {...pageProps} />
            </MessageContextProvider>
          </GlobalState>
        </ThemeProvider>
      </>
    );
  }
}

export default MyApp;

and here is my GlobalState:这是我的 GlobalState:

import React, { useState } from "react";
import PropType from "prop-types";
import { GlobalContext } from "./GlobalContext";
const GlobalState = (props) => {
  const [currentUser, setUser] = useState(props.serverUserData);
  const updateUserAvatar = (picture) => {
    setUser({ ...currentUser, picture: picture });
  };

  const updateUser = (userData) => {
    setUser(userData);
  };
  const [isModalOpen, setModalOpenState] = useState(false);
  const [modalToDisplay, setModalToDisplay] = useState("signup");

  const toggleModal = () => {
    setModalOpenState((prevState) => !prevState);
  };

  const setModal = (name) => {
    setModalToDisplay(name);
  };

  const [loading, setLoadingState] = useState(false);
  const [reloadThisData, setWhatToReload] = useState("");
  const [userProfileId, setUserProfileId] = useState("");
  const [adjustBrightness, setAdjustBrightness] = useState(false);

  const providerValues = {
    isModalOpen,
    toggleModal,
    modalToDisplay,
    setModal,
    currentUser,
    updateUser,
    loading,
    setLoadingState,
    reloadThisData,
    setWhatToReload,
    userProfileId,
    setUserProfileId,
    adjustBrightness,
    setAdjustBrightness,
    updateUserAvatar,
  };

  return (
    <GlobalContext.Provider value={providerValues}>
      {props.children}
    </GlobalContext.Provider>
  );
};

export default GlobalState;

GlobalState.propTypes = {
  children: PropType.object,
  user: PropType.object,
  serverUserData: PropType.object,
};

How can I get rid of this warning?我怎样才能摆脱这个警告? Any idea where is this warning come from?知道这个警告来自哪里吗?

Any idea where is this warning come from?知道这个警告来自哪里吗?

GlobalState.propTypes = {
  children: PropType.object, // <-- here
  user: PropType.object,
  serverUserData: PropType.object,
};

How can I get rid of this warning?我怎样才能摆脱这个警告?

In the simplest way, update the children propType to match what you are rendering, an array of children components.以最简单的方式,更新children propType 以匹配您正在渲染的内容,即一组子组件。

GlobalState.propTypes = {
  children: PropType.array,
  user: PropType.object,
  serverUserData: PropType.object,
};

But generally you want to be more specific:但通常您希望更具体:

GlobalState.propTypes = {
  children: PropType.arrayOf(PropTypes.node), // <-- array of anything renderable
  user: PropType.object,
  serverUserData: PropType.object,
};

But this is redundant since the node type covers arrays as well:但这是多余的,因为node类型也涵盖 arrays :

 // Anything that can be rendered: numbers, strings, elements or an array // (or fragment) containing these types. optionalNode: PropTypes.node,
GlobalState.propTypes = {
  children: PropType.node,
  user: PropType.object,
  serverUserData: PropType.object,
};

If you don't have any restrictions on children types it may be better to just remove children from the propTypes definition altogether.如果您对子类型没有任何限制,最好从propTypes定义中完全删除children类型。

暂无
暂无

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

相关问题 失败的道具类型:提供给“ForwardRef(Grid)”的无效道具“children” - Failed prop type: Invalid prop `children` supplied to `ForwardRef(Grid)` 道具类型失败:提供给“CSSTransition”的道具“children”无效 - Failed prop type: Invalid prop `children` supplied to `CSSTransition` React Native错误失败的道具类型:提供给`Overlay`的`array`类型的无效道具`children`, - React Native error Failed prop type: Invalid prop `children` of type `array` supplied to `Overlay`, 失败的道具类型:提供给“查询”的“数组”类型的无效道具“孩子”,预期的“功能” - Failed prop type: Invalid prop `children` of type `array` supplied to `Query`, expected `function` 失败的道具类型:提供给“重定向”的无效道具“到” - Failed prop type: Invalid prop `to` supplied to `Redirect` Grommet Split children的问题(“警告:失败的prop类型:提供给`Split`的对象类型为&#39;object&#39;的无效prop`children&#39;,需要一个数组。) - Grommet Split children issues(“Warning: Failed prop type: Invalid prop `children` of type `object` supplied to `Split`, expected an array.”) 提供给“InfoWindow”的“array”类型的无效道具“children”,需要一个 ReactElement - Invalid prop 'children' of type 'array' supplied to 'InfoWindow', expected a single ReactElement 失败的道具类型:提供给`DetailField`的&#39;string`类型的prop`child`无效,期望`object` - Failed prop type: Invalid prop `children` of type `string` supplied to `DetailField`, expected `object` 警告:失败的道具类型:提供给`Dropzone`的类型`string`的prop`child`无效,期望的`function` - Warning: Failed prop type: Invalid prop `children` of type `string` supplied to `Dropzone`, expected `function` 警告:道具类型失败:提供给“提供者”的对象类型为“对象”的道具儿童无效,需要一个ReactElement - Warning: Failed prop type: Invalid prop `children` of type `object` supplied to `Provider`, expected a single ReactElement
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM