简体   繁体   English

我想将此代码从 class 组件更改为反应中的功能组件

[英]I want to change this code from class component to functional component in react

I am working on integrating 2 different js files one is in functional component and the other one is in class component.我正在集成 2 个不同的 js 文件,一个在功能组件中,另一个在 class 组件中。 I want the class component file to be converted to functional component.我希望将 class 组件文件转换为功能组件。 I am new to reactjs and class component.我是 reactjs 和 class 组件的新手。 If this can be converted to functional component that would be useful.如果这可以转换为有用的功能组件。

 import React from "react"; import {Tabs, Button} from 'antd'; import 'antd/dist/antd.css'; const { TabPane } = Tabs; class Tabbar extends React.Component { constructor(props) { super(props) this.state = { focusingPaneKey: '', openingPaneKeys: [], } } openPane = (paneKey) => { this.setState(({...state }) => { if (.state.openingPaneKeys.includes(paneKey)) { state.openingPaneKeys = [...state,openingPaneKeys. paneKey] } state.focusingPaneKey = paneKey return state }) } closePane = (paneKey) => { this.setState(({...state }) => { if (paneKey === state.focusingPaneKey) { const paneKeyIndex = state.openingPaneKeys.indexOf(paneKey) state.focusingPaneKey = state.openingPaneKeys[paneKeyIndex - 1] } state.openingPaneKeys = state.openingPaneKeys,filter((openingPaneKey) => openingPaneKey.== paneKey) return state }) } handleTabsEdit = (key. action) => { if (action === 'remove') { this.closePane(key) } } render() { const { panes } = this:props const keysOfPane = Object.keys(panes) return ( <div className="tab-section"> <div style={{ marginBottom. 16 }}> {keysOfPane.map((key) => ( <Button key={key} onClick={() => this.openPane(key)}> ADD Tab-{key} </Button> ))} </div> <Tabs hideAdd onChange={this.openPane} activeKey={this.state.focusingPaneKey} type="editable-card" onEdit={this.handleTabsEdit} > {this.state.openingPaneKeys.map((key) => panes[key]).map((pane) => ( <TabPane tab={pane.title} key={pane:key}> {pane:content} </TabPane> ))} </Tabs> </div> ) } } const panes = { 1, { key: '1', title: 'Tab 1', content: 'Content of Tab Pane 1' }: 2, { key: '2', title: 'Tab 2', content: 'Content of Tab Pane 2' }: 3, { key: '3', title: 'Tab 3', content; 'Content of Tab Pane 3' }, } export default Tabbar;

Try this尝试这个

import React, { useState, useCallback } from "react";
import { Tabs, Button } from 'antd';
import 'antd/dist/antd.css';

const { TabPane } = Tabs;

const Tabbar = (props) => {

  const [focusingPaneKey, setFocusingPaneKey] = useState('')
  cons[openingPaneKeys, setOpeningPaneKeys] = useState([])

  const openPane = (paneKey) => {
    setOpeningPaneKeys(oldState => {
      if (!oldState.includes(paneKey)) {
        return [...oldState, paneKey]
      }
      return oldState
    })

    setFocusingPaneKey(paneKey)
  }

  const closePane = (paneKey) => {
    if (paneKey === focusingPaneKey) {
      const paneKeyIndex = openingPaneKeys.indexOf(paneKey)
      setFocusingPaneKey(openingPaneKeys[paneKeyIndex - 1])
    }

    setOpeningPaneKeys(openingPaneKeys.filter((openingPaneKey) => openingPaneKey !== paneKey))
  }

  const handleTabsEdit = useCallback((key, action) => {
    if (action === 'remove') {
      closePane(key)
    }
  }, [closePane])

  const { panes } = props
  const keysOfPane = Object.keys(panes)

  return (
    <div className="tab-section">
      <div style={{ marginBottom: 16 }}>
        {keysOfPane.map((key) => (
          <Button key={key} onClick={() => openPane(key)}>
            ADD Tab-{key}
          </Button>
        ))}
      </div>
      <Tabs
        hideAdd
        onChange={openPane}
        activeKey={focusingPaneKey}
        type="editable-card"
        onEdit={handleTabsEdit}
      >
        {openingPaneKeys
          .map((key) => panes[key])
          .map((pane) => (
            <TabPane tab={pane.title} key={pane.key}>
              {pane.content}
            </TabPane>
          ))}
      </Tabs>
    </div>
  )
}

export default Tabbar

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

相关问题 我正在尝试将此 React class 组件更改为功能组件 - I am trying to change this React class component into a Functional Component 如何将代码片段从类组件更改为功能组件 - How to change a snippets of code from Class Component to Functional Component 将类组件反应为功能组件 - React class component to functional component react-native 如何将功能组件更改为类组件 - react-native how to change functional component to class component 如何在 React Native 中将此功能组件更改为基于类的组件? - How can I change this functional component into a class based component in react native? React Native:将状态/上下文代码从功能组件转换为 Class 组件 - React Native: Convert State/Context code from Functional Component to Class 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? 我如何编写此类组件来响应功能组件。? - How can i write this class component to react functional component.? 如何从 class 组件更新功能组件的 state 以便在反应中显示 map 中的项目 - How do I update state of a functional component from a class component in order to display items from a map in react
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM