javascript - 未创建 React.js 组件
问题描述
我有一组我想在表格中显示的对象。我有以下代码
class Table extends React.Component {
constructor(props) {
super(props);
this.state = {
data: this.props.data
};
}
render() {
return (
<table>
{this.state.data.map(row => {
console.log(row);
<Test/>
})}
</table>
);
}
}
class Test extends React.Component {
constructor(props) {
super(props);
console.log("7");
}
render() {
return;
}
}
表中的console.log()正确打印出我的所有数据,但是,我的Test构造函数中的console.log()从不打印。
为什么没有创建测试?
我的正确行如下:
class Rows extends React.Component {
constructor(props) {
super(props);
this.state = {
data: this.props.rowData
};
console.log("2");
}
render() {
return (
<tr>
<td>{this.state.data.paymentFrom}</td>
<td>{this.state.data.paymentTo}</td>
<td>{this.state.data.paymentPeriod}</td>
<td>{this.state.data.paymentAmount}</td>
</tr>
);
}
}
解决方案
要正确渲染 Test 组件,您必须返回该组件,如果它永远不会在函数体上返回,则永远不会打印。
试试这个方法:
class Table extends React.Component {
constructor(props) {
super(props);
this.state = {
data: this.props.data
};
}
render() {
return (
<table>
{this.state.data.map(row => {
console.log(row);
return (<Test/>)
})}
</table>
);
}
}
class Test extends React.Component {
constructor(props) {
super(props);
console.log("7");
}
render() {
return;
}
}
推荐阅读
- excel - 从第 3 列第二个单元格开始
- android - 将图像插入条目或编辑器 [Xamarin.Forms]
- python - 如何在文本文件中找到某个单词
- php - Laravel 中是否有类似 withoutOverlapping() 但类似“如果 5 个命令正在运行时不重叠”的功能?
- ios - Converting UnsafeMutableRawPointer to String(utf8) in swift
- reactjs - 只是在父组件中进行查询而不渲染任何东西并将所有数据作为道具发送
- c# - 必须先关闭的 Open DataReader 问题
- c# - Efficient way to pass Bitmap into registered COM component
- html - 难以使 Strava Iframe 响应式 (CSS/HTML)
- php - 根据会话变量显示按钮不起作用