Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

React Hook "useAppDispatch". React Hook names must start with the word "use"

counter1Slice.ts

import { createSlice } from "@reduxjs/toolkit";
import { Counter1State } from "../../model/counter.model";
import { RootState } from "../store";


export const initialValues:Counter1State={
    counter:0,
    loading:false
  }

const counter1Slice = createSlice({
    name:'counter1',
    initialState:{initialValues},
    reducers:{},
    extraReducers:(builder)=>{

    }
})

export const {} = counter1Slice.actions
export const counter1Selector = (store:RootState)=>store.counter1Reducer //this <- store.ts
export default counter1Slice.reducer

store.ts


const reducer = {
  counter1Reducer,
  counter2Reducer,
};

export const store = configureStore({
  reducer,
  devTools: process.env.NODE_ENV === "development",
});

// export type of root state from reducers
export type RootState = ReturnType<typeof store.getState>;
export type AppDispatch = typeof store.dispatch;
export const useAppDispatch = () => useDispatch<AppDispatch>();

page1.tsx

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

import { useSelector } from 'react-redux';
import { counter1Selector } from '../store/slices/counter1Slice';
import { useAppDispatch } from "../store/store";

export default function page1({}: Props) {
  const dispatch = useAppDispatch();                       // red line error useAppDispatch
  const counter1Reducer = useSelector(counter1Selector);   //red line error useSelector

    return (
        <div>
          <h4>Page1</h4>
          <button
            onClick={() => {}}
          >
            counter1 - {''}
          </button>
    
          <button
            onClick={() => {}}
          >
            Async counter1 - {''}
          </button>
        </div>
      );
}

React Hook "useAppDispatch" is called in function "page1" that is neither a React function component nor a custom React Hook function. React component names must start with an uppercase letter. React Hook names must start with the word "use" react-hooks/rules-of-hooks

>Solution :

The name of your component page1 should start with a capital letter (because it’s a component).
Rename it to Page1 and it should work.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading