这是一个反应性的初学者练习,所以我正在寻找最简单的解决方案。我需要将这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;
LIST.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;
ITEM.JS
import React from 'react'
class Item extends React.Component{
render() {
return (
<div>
{this.props.fruit}
</div>
)
}
}
export default Item;
答案 0 :(得分:1)
这是关于如何将React Class组件转换为功能性组件的分步解答,它更好,更干净,更易于阅读:
render
功能this
关键字state
中有Class Component
,请使用hooks
,尤其是useState
或useReducer
钩子lifecycle methods
中使用了Class Component
,则几乎可以在每种情况下使用useEffect
钩子。 (只是需要对此感到满意,您可以进一步了解here和here) 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;
答案 1 :(得分:1)
在这种特定情况下,转换很简单,因为它们是简单的“哑”组件。您只需删除类,将它们的prop作为参数传递给它们,将它们转换为标准函数,然后删除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;
LIST.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;
ITEM.JS
import React from 'react';
function Item({ fruit }) {
return (
<div>
{fruit}
</div>
);
}
export default Item;
答案 2 :(得分: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;
LIST.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;
ITEM.JS
import React from 'react';
const Item = (props) => {props.fruit};
export default Item;