【发布时间】:2025-12-16 17:20:03
【问题描述】:
我正在尝试使用 getVehicleByUser 操作读取一些数据,正如您在图片中看到的那样,我的控制台日志中显示了我的道具,但在 [0].name 之后无法读取我的数据。我知道我的第一个控制台日志是空的 {} 并且在另外两个之后他给了我两倍的数据结果。如果有人现在是怎么回事?我在 app.js<Route exact path="/user/:id" component={UserShow}/> 中也使用了 react-router-dom
import React from "react";
import { connect } from "react-redux";
import { Link } from "react-router-dom";
import * as actions from "../actions";
class UserShow extends React.Component {
componentDidMount(){
this.props.getVehicleByUser(this.props.match.params.id);
}
renderListVehicle(){
console.log(this.props.vehicles); // Work
console.log(this.props.vehicles[0]); //Work
// console.log(this.props.vehicles[0].name) DON'T WORK ?????
}
render() {
return (
<div className="container">
<div className="row">
<div className="col s12 m6">
<div className="card">
{this.renderListVehicle()}
</div>
</div>
</div>
</div>
);
}
}
function mapStateToPros(state) {
return {
authenticated: state.auth.authenticated,
vehicles: state.vehicles
};
}
export default connect(mapStateToPros, actions)(UserShow);
减速器
export default function(state = {}, action){
switch (action.type) {
case GET_MY_VEHICLE:
return action.payload || false;
default:
return state
}
}```
【问题讨论】:
-
您的初始状态如何?还有减速器