简体   繁体   English

错误:渲染的钩子比上一次渲染时更多

[英]Error: Rendered more hooks than during the previous render

I have a component that's rendering on page load and then making some calls to get data client side.我有一个在页面加载时呈现的组件,然后进行一些调用以获取数据客户端。 On initial load everything works as expected.在初始加载时,一切都按预期工作。 However when i click a see more button to make a new call theres an error in the console saying "rendered more hooks than during the previous render".但是,当我单击“查看更多”按钮进行新调用时,控制台中会出现一个错误,提示“比上一次渲染期间渲染了更多的钩子”。 Does anyone have any input on what can be edited here?有没有人对这里可以编辑的内容有任何意见? Because I am a little lost at this point因为此时我有点失落

Error: Rendered more hooks than during the previous render.
    at updateWorkInProgressHook (react-dom.development.js:15115)
    at updateMemo (react-dom.development.js:15583)
    at Object.useMemo (react-dom.development.js:16055)
    at useMemo (react.development.js:1607)
    at SecondCall (SecondCall.js:30)
    at renderWithHooks (react-dom.development.js:14938)
    at updateFunctionComponent (react-dom.development.js:17169)
    at beginWork (react-dom.development.js:18745)
    at HTMLUnknownElement.callCallback (react-dom.development.js:182)
const SomeItems = (guid) => {
  const {
    data: dataR,
    loading: loadingR,
    error: errorR
  } = useQuery(
    'foo',
    {
      variables: { guid }
    }
  );

  const list = dataR?.foo?.relatedProducts || [];
  const urls = list.map((prod) => prod.url);
  const properties = urls.map((url) => url.substr(url.lastIndexOf('/') + 1));
  const firstvalues = properties.slice(0, 3) || [];

  const [flag, setFlag] = useState(false);
  return (
    <div>
      <h4>Shop this Project</h4>
      <div id="values">
        {
          flag
          ? <SecondCall properties={[properties]} />
          : <FirstCall properties={[firstvalues]} />
        }
        <div>
          <button
            id="products"
            type="button"
            onClick={() => setFlag(true)}
          >
            See More
          </button>
        </div>
      </div>
    </div>
  );
}
export const SecondCall = ( properties ) => {
  const valueArray = Object.values(properties);
  const [[destructedValues]] = valueArray;
  const {
    data,
    loading,
    error,
  } = useQuery(
    'foo2',
    {
      variables:{ value: destructedValues}
    }
  );

  if (!data || loading || error) {
    return null;
  }

  const list = data?.foo2?.prods || [];
  const items = list.map((prods) => prods.id);
  const {
    data: dataFLS,
    loading: loadingFLS,
    error: errorFLS,
  } = useQuery(
    'foo3',
    {
      variables: {items}
    }
  );

  if (!dataFLS || loadingFLS || errorFLS) {
    return null;
  }

  const data = dataFLS?.foo3?.prods || [];
  return data.map((products, index) => (
    <div key={`prods-${index}`}>
      <Row key={`related-products-${index}`}>
        "some divs get rendered with data" 
      </Row>
    </div>
  );
};
export const firstCall = ( firstvalues ) => {
  const valueArray = Object.values(firstvalues);
  const [[destructedValues]] = valueArray;
  const {
    data,
    loading,
    error,
  } = useQuery(
    'foo2',
    {
      variables: { value: destructedValues}
    }
  );

  if (!data || loading || error) {
    return null;
  }

  const list = data?.foo2?.prods || [];
  const items = list.map((prods) => prods.id);
  const {
    data: dataFLS,
    loading: loadingFLS,
    error: errorFLS,
  } = useQuery(
    'foo3',
    {
      variables: {items}
    }
  );

  if (!dataFLS || loadingFLS || errorFLS) {
    return null;
  }

  const data = dataFLS?.foo3?.prods || [];
  return data.map((products, index) => {
    return (
      <div key={`prods-${index}`}>
        <Row key={`related-products-${index}`}>
          "some divs get rendered with data" 
        </Row>
      </div>
    );
  );
};

In your SecondCall component, you return null in an if statement.在您的 SecondCall 组件中,您在 if 语句中返回 null。 But since useQuery is a hook, all useQuery statements need to be run before you return anything.但是由于 useQuery 是一个钩子,所以返回任何内容之前都需要运行所有 useQuery 语句。

const {data, loading, error} = useQuery('foo2', {
  variables:{ value: destructedValues}
})

const list = data?.foo2?.prods || [];
const items = list.map((prods) => prods.id);

const {data: dataFLS, loading: loadingFLS, error: errorFLS} = useQuery('foo3', {
  variables: {items}
});

if (!data || loading || error || !dataFLS || loadingFLS || errorFLS) {
    return null;
}

Edit: Apparently you use two useQuery statements in FirsCall too.编辑:显然您也在 FirsCall 中使用了两个 useQuery 语句。 The same logic applies there.同样的逻辑也适用于那里。

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

相关问题 反应钩子错误:渲染的钩子比上一次渲染时更多 - React hooks error: Rendered more hooks than during the previous render 错误:在 React Native 中渲染的钩子比上一次渲染时多 - Error: Rendered more hooks than during the previous render in react native React 中的错误:渲染的钩子比上一次渲染时更多 - Error in React: Rendered more hooks than during the previous render 未捕获(承诺中)错误:渲染的钩子比上一次渲染期间更多 - Uncaught (in promise) Error: Rendered more hooks than during the previous render 错误:渲染了比上一次渲染更多的钩子,handleRemove 导致渲染了更多的钩子错误 - Error: Rendered more hooks than during the previous render, handleRemove causing rendered more hooks error React Hooks 渲染了比上一次渲染更多的钩子 - React Hooks Rendered more hooks than during the previous render Uncaught Invariant Violation:渲染的钩子比上一次渲染时更多 - Uncaught Invariant Violation: Rendered more hooks than during the previous render React native - “渲染的钩子比之前的渲染更多?” - React native - "Rendered more hooks than during the previous render?" Next.JS “渲染的钩子比上一次渲染时更多” - Next.JS “Rendered more hooks than during the previous render” 在 apollo useQuery 之后渲染的钩子比上一次渲染时更多 - Rendered more hooks than during the previous render after apollo useQuery
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM