如何在不使用React hooks的情况下使用Redux Toolkit的查询。

huangapple go评论72阅读模式
英文:

How to use redux toolkit query without React hooks

问题

我已经在Redux Toolkit Query中创建了这个mutation,现在我需要在纯JavaScript文件中调用这个mutation端点。如何做到这一点?

import { apiSlice } from "apiHelpers/ApiSlice";

const placeMyOrder = apiSlice.endpoints.placeMyOrder.initiate();

export const PLACE_order = async () => {
  const { data } = await placeMyOrder(orderobj);
  if (data) {
   
  } else {
    
    return;
  }
};
英文:

I have created this mutation in redux toolkit query and now I have to call this mutation endpoint in vanilla Javascript file. How to do this?

import { apiSlice } from "apiHelpers/ApiSlice";

const placeMyOrder = apiSlice.endpoints.placeMyOrder.initiate();

export const PLACE_order = async () => {
  const { data } = await placeMyOrder(orderobj);
  if (data) {
   
  } else {
    
    return;
  }
};

答案1

得分: 1

"initiate" 是一个 thunk,需要被分发以执行。最好使用 hooks,除非你有特殊的原因,比如服务器端渲染,例如。

以下是如何在没有 hooks 的情况下使用你的查询:

import { apiSlice } from "apiHelpers/ApiSlice";
 
export const getServerSideProps = wrapper.getServerSideProps(
  (store) =>
    async ({ orderInfo }) => {
      const { data: order } = await
          store.dispatch(
              apiSlice.endpoints.placeMyOrder.initiate(orderInfo)
          ); 

      return {
        props: {
          order,
        },
      };
    }
);

以下是如何使用 Redux Toolkit Query 和 hooks:

import { apiSlice } from "apiHelpers/ApiSlice";

// 最好在你的 api 所在的地方执行,然后导出 usePlaceMyOrder
const { usePlaceMyOrder } = apiSlice

export const PlaceOrder: FC<Props> = ({ orderInfo }) => {
  const {
    data: order,
    isFetching,
    isLoading,
  } = usePlaceMyOrder(orderInfo, {
    pollingInterval: 0,
    refetchOnMountOrArgChange: true,
    skip: false,
  })

  if (isLoading) return <div>Loading...</div>
  if (!order) return <div>Missing order!</div>

  return (
    <div>
      {order.name} {isFetching ? '...重新获取中' : ''}
    </div>
  )
}

在组件内部,使用了 usePlaceMyOrder hook,传入了两个参数:orderInfo(传递给组件的数据)和一个选项对象。这个 hook 可能会发起一个 API 请求,以根据给定的 id 下订单,并返回请求的状态和响应数据。

  • 选项对象设置了轮询间隔为 0 毫秒,意味着不会每隔一段时间重复进行 API 请求。
  • refetchOnMountOrArgChange: true 意味着当组件挂载或orderInfo参数变化时,hook 会重新运行 API 请求。
  • skip: false 意味着不会跳过 API 请求。当你不想进行 API 调用时,可以将其设置为 true。

这里可以了解更多关于 RTKQ 的信息

英文:

initiate is thunk it needs to be dispatched to work. It's better to use hooks except you have particular reasons like server side rendering for example.

Here is how to use your query without hooks:

import { apiSlice } from &quot;apiHelpers/ApiSlice&quot;;
 
export const getServerSideProps = wrapper.getServerSideProps(
  (store) =&gt;
    async ({ orderInfo }) =&gt; {
      const { data: order } = await
          store.dispatch(
              apiSlice.endpoints.placeMyOrder.initiate(orderInfo)
          ); 

      return {
        props: {
          order,
        },
      };
    }
);

Here is how to do it with redux toolkit query and hooks:

import { apiSlice } from &quot;apiHelpers/ApiSlice&quot;;

// It&#39;s better to do it where you api is then export usePlaceMyOrder
const { usePlaceMyOrder } = apiSlice

export const PlaceOrder: FC&lt;Props&gt; = ({ orderInfo }) =&gt; {
  const {
    data: order,
    isFetching,
    isLoading,
  } = usePlaceMyOrder(orderInfo, {
    pollingInterval: 0,
    refetchOnMountOrArgChange: true,
    skip: false,
  })

  if (isLoading) return &lt;div&gt;Loading...&lt;/div&gt;
  if (!order) return &lt;div&gt;Missing order!&lt;/div&gt;

  return (
    &lt;div&gt;
      {order.name} {isFetching ? &#39;...refetching&#39; : &#39;&#39;}
    &lt;/div&gt;
  )
}

Inside the component, the usePlaceMyOrder hook is called with two arguments: orderInfo (which is passed to the component) and an options object. This hook probably initiates an API request to place an order with the given id and returns the request's status and response data.

  • The options object sets a pollingInterval of 0 milliseconds, meaning the API request will not be repeated every XX milliseconds.
  • refetchOnMountOrArgChange: true means that the hook will re-run the API request whenever the component mounts or the orderInfo argument changes.
  • skip: false means that the API request will not be skipped. You can set it to true when you do not want to do the API call yet

here is how to dive more into RTKQ

huangapple
  • 本文由 发表于 2023年6月22日 00:35:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76525445.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定