1

src/Service/Post.js

import { createApi, fetchBaseQuery } from "@reduxjs/toolkit/dist/query"
    
    export const postAPi=createApi({
        reducerPath:'postApi',
        baseQuery:fetchBaseQuery({
            baseUrl:"https://jsonplaceholder.typicode.com/",
        }),
        endpoints:(builder)=>({
            getAllPost:builder.query({
                query:()=>({
                    url:'posts',
                    method:'GET'
                })
            })
        })
    })
    export const { useGetAllPostQuery }=postAPi

src/App.js

import { useGetAllPostQuery } from './Service/Post'

export default function App() {
  const data=useGetAllPostQuery()
  return (
    <div>
        <h1>
          React App
        </h1>
    </div>
  )
}

I fetch data using RTK query so,when I try to access useGetAllPostQuery in my app.js react component then it saw me error in browser TypeError: Object(...) is not a function so, anyone can suggest how to solve this error

2 Answers 2

5

You are not importing the query/react endpoint. The normal query endpoint does not create hooks.

import { createApi, fetchBaseQuery } from "@reduxjs/toolkit/query/react"
Sign up to request clarification or add additional context in comments.

3 Comments

thank you sir , I import wrong , now it's working
I used VS code auto import and it's imported from
@HarshadPrajapati importing from @reduxjs/toolkit/query is perfectly fine if you do not use React and do not want the hooks. If you use React, you need to use @reduxjs/toolkit/query/react. Only you as a human can decide that, your auto-import cannot make that decision.
0

A typo can cause the error too. Be careful of the auto-generated hooks.

export const { use_____Query } = ~~APi

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.