There was a build error in npm run due to the inability to destructure the 'store' property from 'useReduxContext2()' when it is null. Additionally, hooks cannot be conditional in Next Js

Next.js with Redux is the framework I am working on, aiming to host the application on Vercel. However, during the local app building process, I'm facing a particular error:

The following error occurs - TypeError: Cannot destructure property 'store' of 'useReduxContext2(...)' as it is null.

Leveraging an if condition can resolve the issue, but unfortunately, using useEffect like hooks under conditional statements is not viable.

The errors encountered are either:

Error: Cannot destructure property 'store' of 'useReduxContext2(...)' as it is null.

or

Hooks cannot be used conditionally.

The consistent error faced reads as follows:

Error: TypeError: Cannot destructure property 'store' of 'useReduxContext2(...)' as it is null.

This problem arises both locally and on Vercel during the build step. Any alternative solution aside from implementing if conditions?

const STATE = useAppSelector((state: RootState) => state)

if (!STATE) {
  return <LoadingPage />
}

const user = useAppSelector((state: RootState) => state.auth);

Answer №1

Enhance the selector with additional logic and ensure that all hooks have completed before returning.

const user = useAppSelector((state: RootState) => state?.auth);
if (!user) return <LoadingPage/> 

It is important to note that I modified state.auth to state?.auth. This change allows it to return undefined if state is undefined, preventing any attempts to access .auth.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Enforcement of Class Initialization in Typescript 2.7

After initializing a sample project using the Angular template in Visual Studio 2017, I made sure to update the package.json file with the latest module versions. However, upon executing the npm install command and navigating to the site, an error related ...

I'm curious about the type I can set for the first parameter of setState in TypeScript. Is there a way to pass a dynamically generated state object to setState?

When trying to pass a newState object to setState and add some additional properties under certain conditions, I encountered a type error: I attempted to define the new State as Pick<ItemListState, keyof ItemListState> but received a type error ...

Ways to address a buffered data problem in Websocket Rxjs. When trying to send a message, it is not being received by the server and instead is being stored in a

Currently, I am utilizing Websocket Rxjs within my application. The connection is successfully established with the server, and upon subscribing to it, all data is received in an array format. However, when attempting to send data back to the server, it se ...

Converting input dates in nest.js using TypeScript formatting

Is it possible to set a custom date format for input in nest.js API request body? For example, like this: 12.12.2022 @ApiProperty({ example: 'ADMIN', description: 'Role name', }) readonly value: string; @ApiProperty({ ...

Exploring Angular2 Guides: Understanding Hierarchical Dependency Injection with a Focus on Service Restoration

As I delved into the Angular2 documentation, a specific example caught my eye in the Hierarchical Dependency Injectors chapter. It introduced me to the Restore service, which enables save/cancel functionality for editing. The code for this service is as f ...

The problem encountered with the Enzyme mount API is a TypeError where it is unable to read the property 'find' of an undefined variable

After converting my component to a stateless one, I started encountering an error that says: TypeError: Cannot read property 'find' of undefined Previously, my tests were running smoothly before the switch. As far as I know, you can test functio ...

having difficulty accessing the value within the Angular constructor

My current issue arises when I click on a button and set a value within the button click method. Despite declaring the variable in the constructor, I am unable to retrieve that value. The code snippet below demonstrates this problem as I keep getting &apos ...

The parameter of type "Construct" cannot be assigned the argument of type "undefined"

I attempted to use code example from the AWS CDK documentation, but it did not function as I had anticipated. Using CDK version 2.62.2 with Typescript. In various parts of the code, a declaration error occurs stating that The argument of type "undefi ...

Using an Interface in a Callback Function with Typescript

Recently, I have started using Typescript and there's something I am uncertain about. I am utilizing an npm package called azure-storage and specifically invoking a method called doesBlobExist: blobService.doesBlobExist(containerName, blobName, (cal ...

Guide on creating elements dynamically with the ngModel attribute in Ionic

I am currently working on dynamically creating an ion-input element when the user clicks on the "+" sign. My goal is to then insert all the input values into an array. While I have successfully created the inputs, I am facing an issue with assigning the [( ...

Cloud Formation from CDK doesn't pause for addDependency to finish

I'm currently in the process of building a CDK stack and I am fairly new to CDK. My goal is to create a Simple Email Service (SES) ConfigurationSet followed by an EmailIdentity. The issue I encountered is that the creation of the EmailIdentity fails d ...

Leverage Prisma's auto-generated types as the input type for functions

Exploring the capabilities of Prisma ORM has led me to experiment with creating models and generating the PrismaClient. Initially, I thought it would be possible to utilize the generated types for variables and response types, but that doesn't seem to ...

The distinctUntilChanged() method is not available for BehaviorSubject

Just delving into the world of Rxjs and attempting to grasp the concept of BehaviourSubject. Here is the code snippet I am working with: export interface State { items: Items[] } const defaultState = { items: [] }; const _store = new BehaviorSub ...

"An error occurred: Uncaught SyntaxError - The import statement can only be used within a module. Including a TypeScript file into a

I need to integrate an Angular 10 TypeScript service into a jQuery file, but I am facing an issue. When I try to import the TypeScript service file into my jQuery file, I encounter the following error: Uncaught SyntaxError: Cannot use import statement outs ...

What is the method in Angular 6 that allows Observable to retrieve data from an Array?

What is the method to retrieve data of an Array using Observable in Angular 6? ob: Observable<any> array = ['a','b','c'] this.ob.subscribe(data => console.log(data)); ...

Struggling to synchronize the newly updated Products List array in zustand?

Let me clarify the scenario I am dealing with so you can grasp it better. I have a Cart and various Products. When a user adds the product (product_id = 1) twice to the cart with the same options (red, xl), I increase the quantity of that item. However, i ...

Leveraging Express Mergeparams in TypeScript

I've run into an issue while working on my small project in Typescript. The problem arises when I attempt to nest my router, as Typescript doesn't seem to acknowledge the parent's parameter. Within the "child" file, I have the following cod ...

Using props in React can be declared either as a `type` or an `interface`

I am working with React code export default class MyComponent extends Component<Props,State> I'm trying to figure out whether I should define my props using a type or an interface. type Props = { isActive: Boolean, onClick: Function } ...

Using ngIf for binding

Trying to bind values based on conditions specified in *ngIf. However, when using the && operator within *ngIf, it seems to be behaving mysteriously. Sample Code: <div *ngIf="days.sunday == true"> <p class="circle ml-3" ...

The different types of property 'cacheLocation' do not match

I have been working on updating an old React app from JavaScript to Typescript gradually. I started by migrating the configuration file, but encountered an error when renaming it to .TS. Here is the error message: /Users/xx/yy/lulo/src/adalConfig.ts (13, ...