rc-tabs not working: type is invalid — expected a string (for built-in components) or a class/function (for composite components)

huangapple go评论59阅读模式
英文:

rc-tabs not working: type is invalid -- expected a string (for built-in components) or a class/function (for composite components)

问题

I used the rc-tabs library in Next.js and got an error. But React.js does not. I don't understand why or the rendering mechanism.
Error: Warning: React.jsx: type is invalid -- expected a string (for built-in components) or a class/function (for composite components)
github: https://github.com/HoTrHieu/Nextjs.git (http://localhost:3000/order)
This is my code:

import React from 'react';
import Tabs, { TabPane } from 'rc-tabs';

const order = () => {
  return (
    <div>
      <Tabs defaultActiveKey="2" onChange={() => {}}>
        <TabPane tab="tab 1" key="1">
          first
        </TabPane>
        <TabPane tab="tab 2" key="2">
          second
        </TabPane>
        <TabPane tab="tab 3" key="3">
          third
        </TabPane>
      </Tabs>
    </div>
  );
};

export default order;

I tried other libraries like react-best-tabs, react-tabs. But still the same error.

I took 2 screenshots of the error:
rc-tabs not working: type is invalid — expected a string (for built-in components) or a class/function (for composite components)
rc-tabs not working: type is invalid — expected a string (for built-in components) or a class/function (for composite components)

struct project

英文:

I used the rc-tabs library in Nextjs and got an error. But Reactjs does not. I don't understand why or the rendering mechanism.
Error: Warning: React.jsx: type is invalid -- expected a string (for built-in components) or a class/function (for composite components)
github: https://github.com/HoTrHieu/Nextjs.git (http://localhost:3000/order)
This is my code:

import React from &#39;react&#39;;
import Tabs, { TabPane } from &#39;rc-tabs&#39;;

const order = () =&gt; {
  return (
    &lt;div&gt;
      &lt;Tabs defaultActiveKey=&quot;2&quot; onChange={() =&gt; {}}&gt;
        &lt;TabPane tab=&quot;tab 1&quot; key=&quot;1&quot;&gt;
          first
        &lt;/TabPane&gt;
        &lt;TabPane tab=&quot;tab 2&quot; key=&quot;2&quot;&gt;
          second
        &lt;/TabPane&gt;
        &lt;TabPane tab=&quot;tab 3&quot; key=&quot;3&quot;&gt;
          third
        &lt;/TabPane&gt;
      &lt;/Tabs&gt;
    &lt;/div&gt;
  );
};

export default order;

I tried other libraries like react-best-tabs, react-tabs. But still the same error

I took 2 screenshots of the error
rc-tabs not working: type is invalid — expected a string (for built-in components) or a class/function (for composite components)

rc-tabs not working: type is invalid — expected a string (for built-in components) or a class/function (for composite components)

struct project

答案1

得分: 0

I've never used this lib before but it looks like rc-tabs have no export of TabPane (and they did not update the docs to reflect this change).

Tabs is not accepting children as well. You should use items props instead. Here is a simple working example to use it:

import React from "react";
import Tabs from "rc-tabs";

const Order = () => {
  const tabs = new Array(8).fill(0).map((_, index) => {
    return {
      key: index.toString(),
      content: `tab content ${index + 1}`,
    };
  });

  return (
    <div>
      <Tabs
        defaultActiveKey='2'
        onChange={() => {}}
        items={tabs.map(({ key, content }) => ({
          key,
          label: `tab ${key}`,
          children: content,
        })}
      />
    </div>
  );
};

export default Order;

(Note: I've retained the code portion in English as you requested.)

英文:

I've never used this lib before but it looks like rc-tabs have no export of TabPane (and they did not update the docs to reflect this change).

Tabs is not accepting children as well. You should use items props instead. Here is a simple working exemple to use it:

import React from &quot;react&quot;;
import Tabs from &quot;rc-tabs&quot;;

const Order = () =&gt; {
  const tabs = new Array(8).fill(0).map((_, index) =&gt; {
    return {
      key: index.toString(),
      content: `tab content ${index + 1}`,
    };
  });

  return (
    &lt;div&gt;
      &lt;Tabs
        defaultActiveKey=&#39;2&#39;
        onChange={() =&gt; {}}
        items={tabs.map(({ key, content }) =&gt; ({
          key,
          label: `tab ${key}`,
          children: content,
        }))}
      /&gt;
    &lt;/div&gt;
  );
};

export default Order;

huangapple
  • 本文由 发表于 2023年3月21日 01:26:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/75793451.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定