Troubleshooting Import Errors When Using Redux Toolkit with React-Redux

richiCoder
2 min readMar 12, 2023

--

If you’re a React developer who’s been using Redux Toolkit to streamline your Redux development, you might have encountered some errors when building your project using npm run build. Specifically, you might have run into issues when importing Redux Toolkit functions like configureStore and createSlice in combination with react-redux.

Fortunately, there’s a workaround for this problem that involves using an alternative import syntax. Instead of importing these functions directly like this:


import { createStore, createSlice } from “@reduxjs/toolkit”
// This way not working sometim

You can import the entire package and then destructure the functions like this:


import pkg from “@reduxjs/toolkit”;
const { createStore, createSlice } = pkg;
// Not a solution at all

However, some developers have reported that this alternative import syntax doesn’t always work as expected. In this case, you can use the following workaround:


import * as pkg from “@reduxjs/toolkit”;
const { createStore, createSlice } = pkg.default ?? pkg;
// This way worked for me in an Astro project
// Which integrates React components
// And Redux Toolkit for managing state

This syntax makes use of the default export of the package and falls back to the package itself if the default export is not available.

By using this workaround, you should be able to import and use Redux Toolkit functions like configureStore and createSlice without encountering any errors. It’s important to note that this workaround is not an official solution, but it has been reported to work for many developers who have encountered similar issues. Just try it!

In conclusion, if you’re using Redux Toolkit with React-Redux and encountering import errors when building (npm run build) your project, try using the alternative import syntax and, if needed, the above workaround to resolve the issue. With these solutions, you can continue to benefit from the power and convenience of Redux Toolkit in your React projects.

--

--