Pass values ​​as parameters from a component or access status in the action creator?

In my project, I have an action creator that depends on the values ​​that are in the application state in order to generate a new value or decide which action to send. My question is to know which way to do this. I thought of two ways. Access these values ​​inside the action creator:

export const changePreviousPage = () => {
    return (dispatch, getState) => {
        let pagination = getState().appReducers.availability.pagination;

        let previousPage = pagination.actualPage != 1 ? pagination.actualPage - 1 : pagination.actualPage;
        dispatch({
            type: types.CHANGE_PREVIOUS_PAGE,
            previousPage
        });  
    }
};

Another option I thought was to pass the value from the component to the action creator:

In my component

class Pagination extends Component {
    ... 
    handlePreviousPage() {
        const {pagination} = this.props;
        this.props.changePreviousPage(pagination);
    }
    ...
} 

In my action creator

export const changePreviousPage = pagination => {
    let previousPage = pagination.actualPage != 1 ? pagination.actualPage - 1 : pagination.actualPage;

    return{
        type: types.CHANGE_PREVIOUS_PAGE,
        previousPage
    }
};

What is the best way to solve it?

+4
source share
1 answer

- / , (, , , , ).

, , store.dispatch. , store.dispatch, .

- , , . , let state = getState() , , ( - API, getState() ).

, ( redux connect helper) , , .

, mapDispatchToProps (, , , handlePreviousPage) ( , , - , ).

0

Source: https://habr.com/ru/post/1648321/


All Articles