忽略代码xD中的西班牙语。我在尝试呈现组件(表的其余部分)时遇到了麻烦。当我执行console.log(clientes)
时,数组中的数据在那里,但是在这一部分中还是会发生
return(
<React.Fragment>
{Object.keys(clientes).map(cliente => (
<Cliente
key={cliente}
info={this.props.clientes[cliente]}
/>
))}
</React.Fragment>
)
这是完整的代码:
import React,{Component} from 'react';
import Cliente from './Cliente';
export default class ListadoClientes extends Component {
mostrarClientes = () => {
const clientes = this.props.clientes;
if(clientes.length === 0) return null;
return(
<React.Fragment>
{Object.keys(clientes).map(cliente => (
<Cliente
key={cliente}
info={this.props.clientes[cliente]}
/>
))}
</React.Fragment>
)
}
render() {
return (
<div className="container col-12">
<table className="table table-striped table-bordered">
<thead className="text-center">
<tr>
<th scope="col" className="align-middle">RAZÓN SOCIAL</th>
<th scope="col" className="align-middle">RUC</th>
<th scope="col" className="align-middle">CORREO ELECTRÓNICO</th>
<th scope="col" className="align-middle">TELÉFONO</th>
<th scope="col" className="align-middle">DIRECCIÓN</th>
<th scope="col" className="align-middle" colSpan="2">ACCIÓN</th>
</tr>
</thead>
<tbody>
{this.mostrarClientes()}
</tbody>
</table>
</div>
);
}
}
这是console.log(clientes)
这是Cliente
import React,{Component} from 'react';
export default class Cliente extends Component {
render() {
const {RAZON_SOCIAL, RUC, DIRECCION, TELEFONO, EMAIL, ID_CLIENTE} = this.props.info;
return (
<tr key={ID_CLIENTE} className="align-middle">
<td>{RAZON_SOCIAL}</td>
<td>{RUC}</td>
<td>{EMAIL}</td>
<td>{TELEFONO}</td>
<td>{DIRECCION}</td>
<td><button value={ID_CLIENTE} className="btn btn-warning">EDITAR</button></td>
<td><button onClick={this.deleteEmpresa} value={ID_CLIENTE} className="btn btn-danger">ELIMINAR</button></td>
</tr>
);
}
}
这是我获取所有数据的地方(我在本地使用api)并将其发送到其他组件:
import React,{Component} from 'react';
import {BrowserRouter, Route, Switch} from 'react-router-dom';
import axios from 'axios';
//COMPONENTES--------------------------
import Header from './Header';
import Clientes from './Clientes';
import Navegacion from './Navegacion';
//-------------------------------------
//API http://192.168.1.146:4181/
export default class Router extends Component {
state = {
clientes: []
}
componentDidMount() {
this.obtenerClientes();
}
//METODOS HTTP
obtenerClientes = () => {
axios.get(`http://192.168.1.146:4181/`)
.then(res => {
this.setState({
clientes: res.data
})
})
}
render() {
return (
<BrowserRouter>
<div className="container-fluid">
<div className="row justify-content-center">
<Header/>
<Navegacion/>
<Switch>
<Route exact path="/" render={() => {
return(
<Clientes
clientes={this.state.clientes}
/>
)
}}/>
</Switch>
</div>
</div>
</BrowserRouter>
);
}
}
答案 0 :(得分:0)
看起来像通过道具传递的“客户”是{data: Array<Cliente>}
类型的对象。
使用this.props.clientes.data.map(cliente=> ...
答案 1 :(得分:0)
您应按如下所示修改代码:
return(
<React.Fragment>
{Object.keys(clientes.data).map(cliente => ( // ==> Modify "clientes" to "clientes.data"
<Cliente
key={cliente}
info={this.props.clientes.data[cliente]} // ==> Modify "clientes" to "clientes.data"
/>
))}
</React.Fragment>
)