Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access the Redux store outside of a component in React

I am beginning with Redux and I always used it in components with connect() and mapStateToProps(), but now I want to call my API with setInterval() every x time to check if the server has new data not stored in the Redux store, and substitute it.

My approach was to create a function that reads the store and update it like that:

import { store } from './dir/dir/store.js'

const refresher = async () => {

    const state = store.getState();

    // Call API, compare 'state.calendar' with calendar from server
    // Call store.dispatch() if they are different, then update Redux store

}

export default refresher

My questions are:

  • Is this a good practise to use Redux?
  • Is there a better approach to this problem?

Thanks

like image 699
J. Diaz Avatar asked Nov 17 '20 22:11

J. Diaz


People also ask

How do I access Redux store outside a component?

You just need to export the store from the module where it created with createStore() . Also, it shouldn't pollute the global window object.

How do you call Redux action outside component?

To trigger a Redux action from outside a component with React, we call store. dispatch from anywhere in our project. import { store } from "/path/to/createdStore"; function testAction(text) { return { type: "TEST_ACTION", text, }; } store.

How do you access Redux store value in a functional component?

redux state can be accessed as prop in a function by using below format. 1: import { connect } from 'react-redux'; // log accepts logMessage as a prop function log(props) { const { environment, logMessage } = props; console. debug('environment' + environment + logMessage ); .... }


Video Answer


1 Answers

It's perfectly fine to export the store and use within a vanilla js/ts file.

Example Redux Store

Make sure to export the store that you create

import { configureStore } from "@reduxjs/toolkit";
import { slice } from "../features/counterSlice";

export const store = configureStore({
  reducer: {
    counter: slice.reducer
  }
});

Usage in Non-Component Code:

You can then import the created store in any other code

import { store } from "../App/store";
import { slice as counterSlice } from "../features/counterSlice";

export function getCount(): number {
  const state = store.getState();
  return state.counter.value;
}

export function incrementCount() {
  store.dispatch(counterSlice.actions.increment());
}

Traditional Usage in Functional Component

import { useDispatch, useSelector } from "react-redux";
import { RootState } from "../App/store";
import { slice as counterSlice } from "../features/counterSlice";

export function Clicker() {
  const dispatch = useDispatch();
  const count = useSelector((state: RootState) => state.counter.value);
  const dispatchIncrement = () => dispatch(counterSlice.actions.increment())
  // ...

Example Slice

import { createSlice } from "@reduxjs/toolkit";

export const slice = createSlice({
  name: "counter",
  initialState: { value: 0 },
  reducers: {
    increment: (state) => {
      state.value += 1;
    }
  }
});

Demo in Codesandbox

Note: You cannot use this option with Server Side Rendering. If you need to support SSR, you can use middleware to listen to dispatched actions and handle elsewhere.

Further Reading

  • What is the best way to access redux store outside a react component? | Stack Overflow
  • Access the Redux Store Outside a React Component | Blog
  • How can I access the store in non react components? | Github Issues
like image 110
KyleMit Avatar answered Oct 14 '22 02:10

KyleMit