英文:
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。
英文:
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 "apiHelpers/ApiSlice";
export const getServerSideProps = wrapper.getServerSideProps(
(store) =>
async ({ orderInfo }) => {
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 "apiHelpers/ApiSlice";
// It's better to do it where you api is then export 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 ? '...refetching' : ''}
</div>
)
}
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论