reactjs - React:为什么这些基本数据不会填充到我的功能组件中?
问题描述
希望你们一切都好。我正在使用一个功能组件,我想用我的数据库中的一些基本数据填充它。我有一个中间层,我已确认将数据发送到前端(ASP.net)。我可以在开发人员工具中看到功能组件本身的数据,但是当我尝试将 props 中的数据渲染到组件中时,它显示为空白。好像变量中根本没有任何东西。
如您所见,数据位于功能组件本身内。功能组件的代码是:
import React from "react";
import PropTypes from "prop-types";
const CompaniesList = ({ Companies }) => {
return (
<React.Fragment>
<div className="height-equal equal-height-lg card">
<div className="card-header">
<h5>Companies List</h5>
</div>
<div className="card-body">
<div className="user-status height-scroll custom-scrollbar">
<table className="table table-borderless">
<thead>
<tr>
<th scope="col" className="pt-0">
Logo
</th>
<th scope="col" className="pt-0">
Company Name
</th>
<th scope="col" className="pt-0">
Industry
</th>
</tr>
</thead>
<tbody>
<tr>
<img src={Companies.logo} alt="logo"></img>
<td>{Companies.companyName}</td>
<td>fdfdf</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</React.Fragment>
);
};
CompaniesList.propTypes = {
Companies: PropTypes.shape({
companyName: PropTypes.string,
id: PropTypes.number,
industry: PropTypes.string,
logo: PropTypes.string,
website: PropTypes.string,
}),
};
export default CompaniesList;
父组件的代码是:
import React from "react";
import getAll from "../../services/companyService";
import { withRouter } from "react-router-dom";
import PropTypes from "prop-types";
import CompaniesList from "./CompaniesList";
const _logger = logger.extend("Companies");
class Companies extends React.Component {
constructor(props) {
super(props);
this.state = {
Companies: {},
};
}
componentDidMount = () => {
this.getListCompanies();
};
getListCompanies = () => {
getAll().then(this.listOfCompaniesSuccess);
};
listOfCompaniesSuccess = (config) => {
let companyList = config.items;
this.setState((prevState) => {
return {
...prevState,
Companies: companyList,
};
});
};
onCompListError = (errResponse) => {
_logger(errResponse);
};
mapCompanies = (Companies) => (
<CompaniesList Companies={Companies} key={Companies.id} />
);
render() {
return (
<React.Fragment>
<div className="row">
<div className="col">{this.mapCompanies(this.state.Companies)}</div>
</div>
<div className="Row">
<div className="col">{this.state.Companies.companyName}</div>
</div>
</React.Fragment>
);
}
}
Companies.propTypes = {
Companies: PropTypes.shape({
companyName: PropTypes.string,
id: PropTypes.number,
industry: PropTypes.string,
logo: PropTypes.string,
website: PropTypes.string,
}),
};
export default withRouter(Companies);
我不是一个非常高级的编码器,但这是一段非常简单的代码。我错过了什么?
解决方案
您的 Companies 道具是一个数组,因此您应该将每个元素映射到表格行。
import React from "react";
import PropTypes from "prop-types";
const CompaniesList = ({ Companies }) => {
return (
<React.Fragment>
<div className="height-equal equal-height-lg card">
<div className="card-header">
<h5>Companies List</h5>
</div>
<div className="card-body">
<div className="user-status height-scroll custom-scrollbar">
<table className="table table-borderless">
<thead>
<tr>
<th scope="col" className="pt-0">
Logo
</th>
<th scope="col" className="pt-0">
Company Name
</th>
<th scope="col" className="pt-0">
Industry
</th>
</tr>
</thead>
<tbody>
{Companies.map((Company) => (
<tr>
<img src={Company.logo} alt="logo"></img>
<td>{Company.companyname}</td>
<td>fdfdf</td>
</tr>
))}
</tbody>
</table>
</div>
</div>
</div>
</React.Fragment>
);
};
推荐阅读
- c++ - 当“运算符”函数根据类而不是实际运算符定义时,它们如何工作?
- r - R foreach 并行和包变量
- javascript - 如何在循环的while或foreach中制作动态的其他选项输入框
- javascript - 在提交事件函数调用上使用“return”时引发错误
- ruby-on-rails - Rails 找不到 docker 应用程序的 rake gem
- javascript - 在Ext JS中捕获网格行的长按或点击保持事件
- php - 如何将存储在一个页面中的href链接中的值发布到下一个php页面
- c++ - 从 ffmpeg 1.1 升级到 3.3 后没有帧解码
- ios - 在 wifi 网络 iOS 中扫描连接的设备
- docusignapi - Docusign 标签值被错误地应用于其他标签