简体   繁体   English

我如何编写此类组件来响应功能组件。?

[英]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所以你想要做的是删除renderconstructor因为它们在功能组件中没有renderconstructor函数,接下来你想做的是使用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.

相关问题 如何编写返回 class 组件作为功能组件的 React 组件? - How can I write React component that returns a class component as a functional component? 反应:如何访问 class 组件中的功能组件的道具 - React: How can I acces to props of a functional component in a class component 我怎样才能把这个 React class 组件变成一个功能组件? - How can i turn this React class component into a functional component? 如何编写扩展功能组件的ES6类React组件? - How to write an ES6 class React Component that extends a functional component? 如何使用反应功能组件添加或删除 class - How can I add or remove class using react functional component 如何反应才能识别它是 class 组件还是功能组件? - How react can Identify it's a class component or functional component? 如何在 React Native 中将此功能组件更改为基于类的组件? - How can I change this functional component into a class based component in react native? 如何将此 Class 组件 function 转换为 React Native 中的功能组件 - How can I convert this Class component function into functional component in React Native 将React库添加到网站时,如何将class组件转换为功能组件? - How can I convert class component into functional component when adding React library to a Website? 将类组件反应为功能组件 - React class component to functional component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM