My components used ReactJs Framework that built with Ant Design UI. My stucking is the rendering on tabs content.
I try to nest the LoginForm
component to TabsCard
component.
Note: the LoginForm component can successful independently rendered without nesting case.
Component rendered on image I attachted:
Here is my code:
TabsCard.js
JavaScript
x
41
41
1
import React, { useState } from 'react';
2
import { Card } from 'antd';
3
import LoginForm from '../LoginForm/index.js';
4
5
function TabsCard() {
6
const tabList = [
7
{
8
key: 'tab1',
9
tab: 'Sign in'
10
},
11
{
12
key: 'tab2',
13
tab: 'Sign up'
14
}
15
];
16
17
const contentList = {
18
tab1: <LoginForm />,
19
tab2: <p>signup</p>,
20
};
21
22
const [activeTab, setActiveTab] = useState('tab1');
23
const handleTabChange = key => {
24
setActiveTab(key);
25
};
26
27
return (
28
<Card
29
style={{ width: '400' }}
30
tabList={tabList}
31
activeTabKey={activeTab}
32
onTabChange={key => {
33
handleTabChange(key);
34
}}
35
>
36
{contentList[setActiveTab]}
37
</Card>
38
);
39
}
40
export default TabsCard;
41
Thank you for your support!
Advertisement
Answer
Change from contentList[setActiveTab]
to contentList[activeTab]
Example of full code
JavaScript
1
41
41
1
import React, { useState } from 'react';
2
import { Card } from 'antd';
3
import 'antd/dist/antd.css';
4
5
function TabsCard() {
6
const tabList = [
7
{
8
key: 'tab1',
9
tab: 'Sign in'
10
},
11
{
12
key: 'tab2',
13
tab: 'Sign up'
14
}
15
];
16
17
const contentList = {
18
tab1: <div>Login form</div>,
19
tab2: <p>signup</p>,
20
};
21
22
const [activeTab, setActiveTab] = useState('tab1');
23
const handleTabChange = key => {
24
setActiveTab(key);
25
};
26
27
return (
28
<Card
29
style={{ width: '400' }}
30
tabList={tabList}
31
activeTabKey={activeTab}
32
onTabChange={key => {
33
handleTabChange(key);
34
}}
35
>
36
{contentList[activeTab]}
37
</Card>
38
);
39
}
40
export default TabsCard;
41
I hope I’ve helped you
Have a nice day!