当我尝试知道类组件中redux中的状态做出反应时,我像三天前一样处理此错误,问题是当我尝试了解状态“ actualPersonAlbum”时,这是我的代码image about the mistake
React class WorkSection extends React.Component {
render() {
const { classes } = this.props;
const {relations} = this.props.relations;
console.log(relations);
const {actualPersonAlbum} = this.props.actualPersonAlbum; //The probleeeeeem
console.log(actualPersonAlbum);
return (
<section style={ sectionStyle }>
</section>
);
}
}
WorkSection.propTypes = {
classes: PropTypes.object
};
const mapStateToProps = state => ({
relations: state.relations,
actualPersonAlbum: state.actualPersonAlbum,
});
export default connect(mapStateToProps) (withStyles(workStyle)(withRouter(WorkSection)));
Redux减速器
import { GET_RELATIONS,
SET_ACTUAL_ALBUM_PERSON } from "../actions/types";
const initialState = {
actualPersonAlbum: "",
};
export default function(state = initialState, action) {
switch (action.type) {
case SET_ACTUAL_ALBUM_PERSON:
return{
actualPersonAlbum:action.payload,
};
default:
return state;
}
}
Redux动作
import {GET_RELATIONS,
SET_ACTUAL_ALBUM_PERSON
} from "./types";
import axios from "axios";
export const setActualAlbumPerson = actualPersonAlbum => {
return {
type: SET_ACTUAL_ALBUM_PERSON,
payload: actualPersonAlbum
}
}
最佳答案
您实际上想做的是破坏actualPersonAlbum
。
如果您希望它起作用,则数据道具应如下所示:
{
actualPersonAlbum: {
actualPersonAlbum: ...,
},
}
您不是只想破坏它一次吗?
const { actualPersonAlbum } = this.props
但是,如果您真的想对该属性进行两次分解,请确保尚未定义
actualPersonAlbum
的可能性(因为它来自异步请求等)。const { actualPersonAlbum } = this.props.actualPersonAlbum || {};
关于javascript - TypeError:无法读取未定义的属性“actualPersonAlbum”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59148083/