Skip to main content
deleted 5 characters in body
Source Link
Mark Rotteveel
  • 110.3k
  • 242
  • 161
  • 234

I need to access to my store outside component (from a classic function) ut, but when I try to call getState(), I have an error:

Property 'getState' does not exist on type '(initialState: any) => any'

Property 'getState' does not exist on type '(initialState: any) => any'

Here is my store declaration and import:

store.ts

import storeHolder from '@lib/storeHolder';
import rootReducer from './rootReducer';
import rootSaga from './rootSaga';

const bindMiddleware = (middleware: any) => applyMiddleware(...middleware);
const composeEnhancers = (typeof window !== 'undefined' && window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__) || compose;

function configureStore(initialState: any) {
  const sagaMiddleware = createSagaMiddleware();
  const store = createStore(rootReducer, initialState, composeEnhancers(bindMiddleware([sagaMiddleware]))) as any;

  store.sagaTask = sagaMiddleware.run(rootSaga);

  storeHolder.setStore(store);

  return store;
}

export default configureStore;

file-where-i-need.ts

import store from '@redux/store';
import moment from 'moment';

export function formatDateFromnow(date: Date) {
  const state = store.getState(); <-- error: Property 'getState' does not exist on type '(initialState: any) => any'

  const { locale } = state.theme.locale;  

  return moment(date).locale(locale).fromNow();
}

Any idea of what I did wrong,? I read a lot of posts about this, but did not foundfind a solution.

Thanks.

I need to access to my store outside component (from a classic function) ut when I try to call getState(), I have an error:

Property 'getState' does not exist on type '(initialState: any) => any'

Here is my store declaration and import:

store.ts

import storeHolder from '@lib/storeHolder';
import rootReducer from './rootReducer';
import rootSaga from './rootSaga';

const bindMiddleware = (middleware: any) => applyMiddleware(...middleware);
const composeEnhancers = (typeof window !== 'undefined' && window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__) || compose;

function configureStore(initialState: any) {
  const sagaMiddleware = createSagaMiddleware();
  const store = createStore(rootReducer, initialState, composeEnhancers(bindMiddleware([sagaMiddleware]))) as any;

  store.sagaTask = sagaMiddleware.run(rootSaga);

  storeHolder.setStore(store);

  return store;
}

export default configureStore;

file-where-i-need.ts

import store from '@redux/store';
import moment from 'moment';

export function formatDateFromnow(date: Date) {
  const state = store.getState(); <-- error: Property 'getState' does not exist on type '(initialState: any) => any'

  const { locale } = state.theme.locale;  

  return moment(date).locale(locale).fromNow();
}

Any idea of what I did wrong, I read a lot of posts about this but not found a solution.

Thanks.

I need to access to my store outside component (from a classic function), but when I try to call getState(), I have an error:

Property 'getState' does not exist on type '(initialState: any) => any'

Here is my store declaration and import:

store.ts

import storeHolder from '@lib/storeHolder';
import rootReducer from './rootReducer';
import rootSaga from './rootSaga';

const bindMiddleware = (middleware: any) => applyMiddleware(...middleware);
const composeEnhancers = (typeof window !== 'undefined' && window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__) || compose;

function configureStore(initialState: any) {
  const sagaMiddleware = createSagaMiddleware();
  const store = createStore(rootReducer, initialState, composeEnhancers(bindMiddleware([sagaMiddleware]))) as any;

  store.sagaTask = sagaMiddleware.run(rootSaga);

  storeHolder.setStore(store);

  return store;
}

export default configureStore;

file-where-i-need.ts

import store from '@redux/store';
import moment from 'moment';

export function formatDateFromnow(date: Date) {
  const state = store.getState(); <-- error: Property 'getState' does not exist on type '(initialState: any) => any'

  const { locale } = state.theme.locale;  

  return moment(date).locale(locale).fromNow();
}

Any idea of what I did wrong? I read a lot of posts about this, but did not find a solution.

Source Link

Can't access directly to my redux store outside component

I need to access to my store outside component (from a classic function) ut when I try to call getState(), I have an error:

Property 'getState' does not exist on type '(initialState: any) => any'

Here is my store declaration and import:

store.ts

import storeHolder from '@lib/storeHolder';
import rootReducer from './rootReducer';
import rootSaga from './rootSaga';

const bindMiddleware = (middleware: any) => applyMiddleware(...middleware);
const composeEnhancers = (typeof window !== 'undefined' && window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__) || compose;

function configureStore(initialState: any) {
  const sagaMiddleware = createSagaMiddleware();
  const store = createStore(rootReducer, initialState, composeEnhancers(bindMiddleware([sagaMiddleware]))) as any;

  store.sagaTask = sagaMiddleware.run(rootSaga);

  storeHolder.setStore(store);

  return store;
}

export default configureStore;

file-where-i-need.ts

import store from '@redux/store';
import moment from 'moment';

export function formatDateFromnow(date: Date) {
  const state = store.getState(); <-- error: Property 'getState' does not exist on type '(initialState: any) => any'

  const { locale } = state.theme.locale;  

  return moment(date).locale(locale).fromNow();
}

Any idea of what I did wrong, I read a lot of posts about this but not found a solution.

Thanks.