Uncaught TypeError: Cannot read properties of undefined (reading 'map')
Выдаёт в консоли ошибку при втором рендеринге компонента (после авторизации/регистрации и переходе на главную страницу). Прошерстил множество похожих вопросов и пробовал различные варианты решения, но ни один пока не помог должным образом. Максимум избавляешься от этой ошибки в консоли, но данные массивов не выводятся.
Сам компонент:
import React, { Component } from 'react';
import { connect } from 'react-redux';
import './shop.scss';
import banner from '../../../img/banner.png';
import {devicesLoaded} from '../../../actions';
import Spinner from '../../spinner';
import ErrorIndicator from '../../error-indicator';
import DeviceItem from '../../deviceItem/deviceItem';
class Shop extends Component {
render() {
const {devices, types, loading, error, devicesLoaded} = this.props;
if (loading) {
return <Spinner />;
}
if (error) {
return <ErrorIndicator />;
}
return (
<section className='shop'>
<div className='shop__banner'>
<img src={banner} alt='banner' />
</div>
<div className='shop__wrapper'>
<div className='shop__wrapper_type'>
<React.Fragment>
{types.map((type) => {
if (type.id === 2) {return type.name}
})}
</React.Fragment>
</div>
<div className='shop__wrapper_devices'>
<React.Fragment>
{devices.map((device) => {
return <DeviceItem key={device.id} device={device} />
})}
</React.Fragment>
</div>
</div>
</section>
);
}
};
const mapStateToProps = ({deviceList: {devices, types, loading, error}}) => {
return {devices, types, loading, error};
};
const mapDispatchToProps = (dispatch) => {
return {
devicesLoaded: (newDevices) => dispatch(devicesLoaded(newDevices))
};
};
export default connect(mapStateToProps, mapDispatchToProps)(Shop);
Редьюсер:
const updateDeviceList = (state, action) => {
if (state === undefined) {
return {
devices: [
{
id: 1,
name: 'Apple BYZ S852I',
price: 750,
img: 'ab4a63f8-66a0-401e-838e-20278e6343bc.jpg',
typeId: 2
},
{
id: 2,
name: 'Apple EarPods MNHF2ZMA',
price: 2299,
img: '34e793c2-5ff4-45cf-bf88-26658cbffbea.jpg',
typeId: 2
},
],
types: [
{
id: 2,
name: 'Наушники'
},
{
id: 1,
name: 'Беспроводные наушники'
}
],
loading: false,
error: null
};
};
switch (action.type) {
case 'DEVICES_LOADED':
return {
devices: action.payload
};
default:
return state;
}
}
export default updateDeviceList;
Главный редьюсер:
const reducer = (state, action) => {
return {
deviceList: updateDeviceList(state, action),
userState: updateUserState(state, action)
};
};