在React组件中处理深度嵌套的数据

时间:2019-03-20 01:05:14

标签: reactjs redux react-redux

我的组件面临一个难题,即我要访问的数据尚未“存在”。例如,当我记录我的redux操作时,我得到以下信息: enter image description here enter image description here

第一张图片是我的数据在加载时的样子,第二张图片是我的嵌套数据在接收时的样子。

因此,我必须在组件的render方法内部运行检查,例如:

const data = homepage.upcomingTrips.data.upcoming_hikes;
{ !loading && data && data.upcoming_hikes &&
    ...more code here
}

_reducers / homepage / upcoming_trips.reducer.js:

import { homepageConstants } from '../../_constants';

export function upcomingTrips(state = {}, action) {

switch (action.type) {
    case homepageConstants.UPCOMING_TRIPS_REQUEST:
        return {
            data: {},
            loading: true,
        };
    case homepageConstants.UPCOMING_TRIPS_SUCCESS:
        return {
            data: action.data,
            loading: false,
        };
    case homepageConstants.UPCOMING_TRIPS_FAILURE:
        return {
            error: action.error,
        };

    default:
        return state;
    }
}

_reducers / homepage / index.js:

import { combineReducers } from 'redux';

import { currentPack } from './current_pack.reducer';
import { totalMiles } from './total_miles.reducer';
import { upcomingTrips } from './upcoming_trips.reducer';

const homepage = combineReducers({
    currentPack,
    totalMiles,
    upcomingTrips,
});

export default homepage;

_reducers / index.js

import { combineReducers } from 'redux';

import { authentication } from './authentication.reducer';
import { registration } from './registration.reducer';
import { users } from './users.reducer';
// import { homepageData } from './homepage.reducer';
import homepage from './homepage';
import { explorepageData } from './explorepage.reducer';
import { alert } from './alert.reducer';

const rootReducer = combineReducers({
  authentication,
  registration,
  users,
  homepage,
  explorepageData,
  alert
});

export default rootReducer;

UpcomingTrips.jsx:

class UpcomingTrips extends Component {

    componentDidMount() {
        this.props.getUpcomingTrips();
    }

    render() {
        const { classes, homepage } = this.props;
        const data = homepage.upcomingTrips.data.upcoming_hikes;
        const loading = homepage.upcomingTrips.data.loading;
        console.log('homepage: ', homepage.upcomingTrips.data.upcoming_hikes);

        return (
            <UpcomingTripStyles>
                <Typography variant='h4' className={ `${ classes.headline } ${ classes.widgetName }` }>
                    Upcoming Trips
                </Typography>
                { loading && <img src='/static/loading.svg' alt='loading' className='loading-img' /> }
                <ul className='upcoming-hikes'>
                    { !loading && data && data.upcoming_hikes &&
                        data.upcoming_hikes.map((h) =>
                            <li className='hike-item' key={ h.id }>
                            <span className='start-date'>{ toDate(h.start_date) }</span>
                            <span className='hike-name'>{ h.name }</span>
                            </li>,
                        )
                    }
                    { !loading && data && !_.toArray(data.upcoming_hikes).length &&
                        <p>You haven't created any Upcoming Hikes yet!</p>
                    }
                </ul>
            </UpcomingTripStyles>
        );
    }
}

function mapStateToProps(state) {
    const { homepage } = state;
    return {
        homepage,
    };
}

const mapDispatchToProps = dispatch => {
    return {
        getUpcomingTrips: () => {
            dispatch(homepageActions.getUpcomingTrips())
        }
    };
};

const enhance = compose(
    connect(mapStateToProps, mapDispatchToProps),
    withStyles(styles),
);

const connectedUpcomingTrips = enhance(UpcomingTrips);
export { connectedUpcomingTrips as UpcomingTrips };

那么处理所有这些问题的最佳方法是什么?我是否应该考虑使用Normalizr来展平数据?还是与我的减速器的工作方式有更多关系?

0 个答案:

没有答案