gpt4 book ai didi

reactjs - ReactJS instance.render不是函数

转载 作者:行者123 更新时间:2023-12-03 08:32:11 26 4
gpt4 key购买 nike

我尝试学习ReactJS并遵循以下指南:https://www.taniarascia.com/getting-started-with-react/,但是我在STATE部分遇到问题,无法继续。
我在App.js中编写的:

import React, {Component} from 'react'
import Table from './table'

class App extends Component {
state = {
characters: [
{
name: 'Charlie',
job: 'Janitor',
},
{
name: 'Mac',
job: 'Bouncer',
},
{
name: 'Dee',
job: 'Aspring actress',
},
{
name: 'Dennis',
job: 'Bartender',
},
],
}
removeCharacter = (index) => {
const {characters} = this.state

this.setState({
characters: characters.filter((character, i) => {
return i !== index
}),
render() {
const { characters } = this.state

return (
<div className="container">
<Table characterData={characters} removeCharacter={this.removeCharacter} />
</div>
)
}
})

}
}
export default App
和IN TABLE.JS
import React from 'react'

const TableHeader = () => {
return (
<thead>
<tr>
<th>Name</th>
<th>Job</th>
</tr>
</thead>
)
}

const TableBody = (props) => {
const rows = props.characterData.map((row, index) => {
console.log(index);
return (
<tr key={index}>
<td>{row.name}</td>
<td>{row.job}</td>
<td>
<button onClick={() => props.removeCharacter(index)}>Delete</button>
</td>
</tr>
)
})
return <tbody>{rows}</tbody>
}

class Table extends Component {
render() {
// const {characterData} = this.props

// return (
// <table>
// <TableHeader />
// <TableBody characterData={characterData} />
// </table>
// )
const Table = (props) => {
const {characterData, removeCharacter} = props

return (
<table>
<TableHeader />
<TableBody characterData={characterData} removeCharacter={removeCharacter} />
</table>
)
}
}


export default Table
,因此结果是:
TypeError:instance.render不是函数
预先感谢谁会回答我

最佳答案

重构工作代码
App.js

import React, { Component } from "react";
import Table from "./Table";

class App extends Component {
state = {
characters: [
{
name: "Charlie",
job: "Janitor",
},
{
name: "Mac",
job: "Bouncer",
},
{
name: "Dee",
job: "Aspring actress",
},
{
name: "Dennis",
job: "Bartender",
},
],
};
removeCharacter = (index) => {
const { characters } = this.state;

this.setState({
characters: characters.filter((character, i) => {
return i !== index;
}),
});
};

render() {
const { characters } = this.state;

return (
<div className="container">
<Table
characterData={characters}
removeCharacter={this.removeCharacter}
/>
</div>
);
}
}
export default App;

Table.js
import React from "react";

const TableHeader = () => {
return (
<thead>
<tr>
<th>Name</th>
<th>Job</th>
</tr>
</thead>
);
};

const TableBody = (props) => {
const rows = props.characterData.map((row, index) => {
console.log(index);
return (
<tr key={index}>
<td>{row.name}</td>
<td>{row.job}</td>
<td>
<button onClick={() => props.removeCharacter(index)}>Delete</button>
</td>
</tr>
);
});
return <tbody>{rows}</tbody>;
};
class Table extends React.Component {
render() {
const Table = () => {
const { characterData, removeCharacter } = this.props;

return (
<table>
<TableHeader />
<TableBody
characterData={characterData}
removeCharacter={removeCharacter}
/>
</table>
);
};
return (
<div>
<Table />
</div>
);
}
}

export default Table;

关于reactjs - ReactJS instance.render不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65843388/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com