[英]How can i write this class component to react functional component.?
Iam trying to write my component to react function but i cant format it properly and my classes and styles dosent apllied on it what is the proper way to convert this component.我正在尝试编写我的组件来响应函数,但我无法正确格式化它,并且我的类和样式不适合它什么是转换此组件的正确方法。 My all other code is in a fuctions and i also want this to be a functional component
我的所有其他代码都在功能中,我也希望这是一个功能组件
My Component我的组件
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import Tab from './Tab';
class Tabs extends Component {
static propTypes = {
children: PropTypes.instanceOf(Array).isRequired,
}
constructor(props) {
super(props);
this.state = {
activeTab: this.props.children[0].props.label,
};
}
onClickTabItem = (tab) => {
this.setState({ activeTab: tab });
}
render() {
const {
onClickTabItem,
props: {
children,
},
state: {
activeTab,
}
} = this;
return (
<div className="tabs">
<ol className="tab-list">
{children.map((child) => {
const { label } = child.props;
return (
<Tab
activeTab={activeTab}
key={label}
label={label}
onClick={onClickTabItem}
/>
);
})}
</ol>
<div className="tab-content">
{children.map((child) => {
if (child.props.label !== activeTab) return undefined;
return child.props.children;
})}
</div>
</div>
);
}
}
export default Tabs;
import React, { useState } from 'react';
import PropTypes from 'prop-types';
import Tab from './Tab';
export default function Tabs ({ children }){
const [activeTab, setActiveTab] = useState(children[0].props.label);
function onClickTabItem(tab){
setActiveTab({ activeTab: tab });
}
return (
<div className="tabs">
<ol className="tab-list">
{children.map((child) => {
const { label } = child.props;
return (
<Tab
activeTab={activeTab}
key={label}
label={label}
onClick={onClickTabItem}
/>
);
})}
</ol>
<div className="tab-content">
{children.map((child) => {
if (child.props.label !== activeTab) return undefined;
return child.props.children;
})}
</div>
</div>
);
};
Tabs.propTypes = {
children: PropTypes.node.isRequired
}
So what you wanna do is remove the render
and the constructor
since their is no render
and constructor
in functional component and the next thing you wanna do is use the useState
hook to maintain the state variables.Furthermore , you could pull out children
from the props
passed to the components and for static properties on a function you declare them on the function itself so instead of using static propTypes
you would use Tabs.propTypes
所以你想要做的是删除
render
和constructor
因为它们在功能组件中没有render
和constructor
函数,接下来你想做的是使用useState
钩子来维护状态变量。此外,你可以从props
取出children
传递给组件和函数上的静态属性,您在函数本身上声明它们,因此您将使用Tabs.propTypes
而不是使用static propTypes
Tabs.propTypes
import React from "react";
import PropTypes from "prop-types";
import Tab from "./Tab";
const Tabs = props => {
const { children } = props;
const [state, setState] = useState({
activeTab: props.children[0].props.label
});
const onClickTabItem = tab => {
setState({ activeTab: tab });
};
return (
<div className='tabs'>
<ol className='tab-list'>
{children.map(child => {
const { label } = child.props;
return (
<Tab
activeTab={state.activeTab}
key={label}
label={label}
onClick={()=>onClickTabItem()}
/>
);
})}
</ol>
<div className='tab-content'>
{children.map(child => {
if (child.props.label !== activeTab) return undefined;
return child.props.children;
})}
</div>
</div>
);
};
//Static Props
Tabs.propTypes = {
children: PropTypes.instanceOf(Array).isRequired
};
export default Tabs;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.