简体   繁体   English

如何在使用 ReactJS 下新订单时播放声音

[英]How to play a sound when a new order is placed using ReactJS

I am building this PWA with ReactJS.我正在用 ReactJS 构建这个 PWA。

As the title says, I just want to play a sound whenever a new order is placed.正如标题所说,我只想在下新订单时播放声音。

I am using the Context API in order to manage the states.我正在使用 Context API 来管理状态。

This is my OrderContext.js where I fetch the orders from the API using React Query.这是我的OrderContext.js ,我使用 React Query 从 API 获取订单。

import React, { createContext } from "react";
import { useQuery } from "react-query";
import apiClient from "../EventService";

export const OrderContext = createContext();

export const OrderProvider = (props) => {
  const fetchOrders = async () => {
    const { data } = await apiClient.getEvents();
    return data;
  };

  let { data: orders, status } = useQuery("orders", fetchOrders, {
    refetchInterval: 15000,
    refetchIntervalInBackground: true,
    notifyOnStatusChange: false,
  });

  return (
    <OrderContext.Provider value={[orders, status]}>
      {props.children}
    </OrderContext.Provider>
  );
};

Then somewhere in my OrdersList.js component, I just use the state and map through it:然后在我的OrdersList.js组件中的某个地方,我只使用状态并通过它映射:

{orders.map((order) => (
     <Order key={order.id} order={order} />
))}

And lastly, in my Order.js component is where I check if there is a new order or not:最后,在我的Order.js组件中,我检查是否有新订单:

I am using this function to check if the order has been placed 15 or less minutes ago.我正在使用此功能来检查订单是否在 15 分钟或更短时间前下达。

const isNewOrder = (orderCreatedDate) => {
    if (moment().diff(moment(orderCreatedDate)) / 900000 <= 1) {
      return true;
    } else {
      return false;
    }
  };

and then in the in the JSX of the component I simply do this to display a badge on the new orders:然后在组件的 JSX 中,我只是这样做以在新订单上显示徽章:

  {isNewOrder(order.date_created) && (
        <Badge style={{ padding: 3 }} content="Nuevo" />
   )}

I am having troubles adding a sound effect to these new orders, I tried using Howler but couldn't make it work properly.我在为这些新订单添加音效时遇到了麻烦,我尝试使用 Howler 但无法使其正常工作。

If someone could help me or maybe show me a way to achieve this I would really appreciate.如果有人可以帮助我或向我展示实现这一目标的方法,我将不胜感激。 I can provide the full components if needed, let me know.如果需要,我可以提供完整的组件,让我知道。 Thanks in advance.提前致谢。

Have a nice day!祝你今天过得愉快!

I think your best approach is to add a prop to your Order component and check if you are on the order list page or on the order page.我认为您最好的方法是向您的Order组件添加一个道具,并检查您是否在订单列表页面或订单页面上。 If on one single order then you call sound.play , otherwise you do not.如果在一个订单上,则调用sound.play ,否则不调用。

Also, you should use React Howler if you're not already.此外,如果您还没有使用React Howler,则应该使用它。 Regular Howler does not play well (no pun intended) with React.常规咆哮在 React 中表现不佳(无意双关语)。

Finally, you should add a componentWillUnmount function to stop the sound from playing (just call sound.stop() inside componentWillUnmount ).最后,您应该添加一个componentWillUnmount函数来停止播放声音(只需在componentWillUnmount内调用sound.stop() )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM