小编enr*_*rij的帖子

RTK 查询“use*Query”使用缓存的 isLoading 和数据,即使对于不同的参数

我是 RTK 查询的新手,在使用不同参数的查询之间共享数据时遇到问题。

// api.js
const { useGetStuffQuery } = api.injectEndpoints({
  endpoints: (builder) => ({
    getStuff: builder.query({
      query: (stuffId) => ({ url: `http://some.random-url.com/${stuffId}` }),
    }),
  }),
});

// component.js
function(props) {
  const [id, setId] = useState(1);
  const { data, isLoading } = useGetStuffQuery(id);

  if (val === '2') {
    console.log('datttt', data); // this initially prints data from id:1
  }

  return <div>
      <input value={id} onChange={(e) => setId(e.target.value)} />
      {
        isLoading ? <span>loading...</span>
                  : <span>data is: {data}</span>
      }

  </div>
}
Run Code Online (Sandbox Code Playgroud)

当我将 id 从 …

redux redux-toolkit rtk-query

11
推荐指数
1
解决办法
1万
查看次数

标签 统计

redux ×1

redux-toolkit ×1

rtk-query ×1