Search code examples
reactjsreduxreact-reduxrtk-query

TypeError: Object(...) is not a function in react


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


Solution

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

    import { createApi, fetchBaseQuery } from "@reduxjs/toolkit/query/react"