1

这是一个反应初学者练习,所以我正在寻找最简单的解决方案。我需要将这 3 个类组件转换为功能组件。我目前正在学习 React,所以任何有用的评论也将不胜感激。提前致谢!

APP.JS

import React from 'react'
import List from './components/List'


class DataArr extends React.Component {

  render() {

    const fruits = ["banana", "apple", "pear", "melon", "lemon"]

    return (
    <div className="DataArr">
      <List fruits={fruits}/>
    </div>
    )
}
}

export default DataArr;

列表.JS

import React from "react";

import Item from "./Item"; 

class List extends React.Component {

  render() {

    return (
      <div>
        {this.props.fruits.map((fruit, index) => {
          return <Item key={index} fruit={fruit} />;
        })}
      </div>
    );
  }
}
export default List;

项目.JS

import React from 'react'
class Item extends React.Component{
    render() {

        return (
              <div>
                {this.props.fruit}
              </div>
        )
      }
    }
    export default Item;
4

3 回答 3

1

这是关于如何将 React 类组件转换为功能组件的逐步答案,它更好、更清晰、更易于阅读:

  1. 您需要将类更改为函数
  2. 移除render功能
  3. 删除this关键字
  4. 如果您有state在您的Class Component使用hooks中特别是useStateuseReducer
  5. 如果你用lifecycle methods在你的,Class Component你几乎可以useEffect在任何情况下使用钩子。(只需要对它感到满意,您可以在此处此处阅读更多相关信息)

App.js将会:


import React from 'react'
import List from './components/List'


// class DataArr extends React.Component { //<-- Remove this line
const DataArr = () => { // <-- Create a function Component
  // render() { // <-- remove render function because you don't need it
    const fruits = ["banana", "apple", "pear", "melon", "lemon"]
    return (
      <div className="DataArr">
        <List fruits={fruits}/>
      </div>
    )
// } // this curly brace is for render function
}

export default DataArr;

List.js将会:

import React from "react";

import Item from "./Item"; 

// class List extends React.Component { //<-- Remove this line

const List = (props) => {
// render() { // <-- remove render function because you don't need it
    return (
      <div>
        {
          // this.props.fruits.map((fruit, index) => { <-- Change this.props to props
          props.fruits.map((fruit, index) => {
          return <Item key={index} fruit={fruit} />;
        })}
      </div>
    );
  // } // this curly brace is for render function
}
export default List;

ITEM.js是这样的:

import React from 'react'
// class Item extends React.Component{ //<-- Remove this line
const Item = (props) => { // <-- Create a function Component
  // render() { // <-- remove render function because you don't need it
    return (
          <div>
            {
              // this.props.fruit // <-- change this.props to props
              props.fruit
            }
          </div>
    )
}
// } // this curly brace is for render function
export default Item;
于 2020-06-08T13:16:41.433 回答
1

在这种特殊情况下,转换是直接的,因为它们是简单的“哑”组件。您只需删除类,将它们转换为标准函数,并将它们的 props 作为参数传递,删除render()并替换为return.

APP.JS

import React from 'react'
import List from './components/List'

function DataArr() {
    const fruits = ["banana", "apple", "pear", "melon", "lemon"];

    return (
        <div className="DataArr">
          <List fruits={fruits}/>
        </div>
    );
}

export default DataArr;

列表.JS

import React from "react";

import Item from "./Item"; 

function List({ fruits }) {
    return (
      <div>
        {fruits.map((fruit, index) => {
          return <Item key={index} fruit={fruit} />;
        })}
      </div>
    );
}

export default List;

项目.JS

import React from 'react';

function Item({ fruit }) {
    return (
        <div>
            {fruit}
        </div>
    );
}

export default Item;
于 2020-06-08T13:16:46.283 回答
0

APP.JS

    import React from 'react';
    import List from './components/List';

    const DataArr = () => {
        const fruits = ["banana", "apple", "pear", "melon", "lemon"];
        return (
            <div className="DataArr">
                <List fruits={fruits} />
            </div>
        )
    }
    export default DataArr;

列表.JS

import React from 'react';
import Item from './Item';

const List = (props) =>
        {props.fruits.map(fruit, index) => 
              <Item key={index} fruit={fruit} />};
export default List;

项目.JS

import React from 'react';

const Item = (props) => {props.fruit};

export default Item;
于 2020-06-08T13:38:19.283 回答