How can you access the recess repository from an incompetent helper function?

I have a helper function that I call when I want to remove something from my redux store. However, I need to have access to the current store inside the function to determine what to do next. Here is what I want to do:

export function deleteDocument (id) {
    this.props.dispatch(deleteShape({id}));

    const getStore = getStore(); //<-- I need something like this

    if(getStore.documents && !getStore.documents.find(doc => doc.selected)){
        this.props.dispatch(makeTopDocumentSelected());
    }
}

I call this function from the component and pass the "this" context so that it has access to send, if you are interested. But if I try to use the link that I pass, it does not update after the call to "deleteShape", because this function is not "connected" (due to the lack of a better word) to the reduction storage. So my question is this: how can I access the current repository of reducts from non-component functions? Thanks

+4
source share
1 answer

I have to say that I consider it improper practice to randomly access the repository from some function, but if you need to do this, this is possible:

file: storeProvider.js

var store = undefined;

export default {
    init(configureStore){
        store = configureStore();
    },
    getStore(){
        return store;
    }
};

file: App.js

import { createStore } from 'redux';
import rootReducer from './rootReducer';
import storeProvider from './storeProvider';

const configureStore = () => createStore(rootReducer);
storeProvider.init(configureStore);
const store = storeProvider.getStore();

const App = () =>
    <Provider store={store} >
        <Stuff/>
    </Provider>

file: yourfunction.js

import storeProvider from './storeProvider';

export function deleteDocument (id) {
    this.props.dispatch(deleteShape({id}));

    const state = storeProvider.getStore().getState();

    if(state.documents && !state.documents.find(doc => doc.selected)){
        this.props.dispatch(makeTopDocumentSelected());
    }
}
+4
source

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


All Articles