【问题标题】:React-TypeScript: Expected 0 arguments, but got 1React-TypeScript:预期 0 个参数,但得到 1 个
【发布时间】:2022-01-24 20:33:20
【问题描述】:

我不确定为什么会出现此错误。所以基本上我将 id 从 Product 组件发送到 getProduct redux 操作。不知道为什么它不起作用。

// 产品组件

    const Product: React.FC = () => {
       const { id } = useParams();
    
       const dispatch = useDispatch();
       useEffect(() => {
          dispatch(getProduct(id));
       }, [dispatch, id]);
    
       return (
          <section className="product">
             <div className="bd-container product-container"></div>
          </section>
       );
    };
export default Product;

// 获取产品还原操作

interface productId {
   id: string | undefined;
}
export const getProduct =
   (id: productId) => async (dispatch: Dispatch<Actions>) => {
      dispatch({
         type: actionTypes.GET_PRODUCT_LOADING,
      });

      try {
         const { data } = await axios.get(`${url}/api/products/${id}`);
         dispatch({
            type: actionTypes.GET_PRODUCT_SUCCESS,
            payload: data,
         });
      } catch (error: any) {
         dispatch({
            type: actionTypes.GET_PRODUCT_FAIL,
            payload: error,
         });
      }
   };

【问题讨论】:

    标签: reactjs redux react-redux


    【解决方案1】:

    动作对象或动作创建函数都可以传递给您从useDispatch() 获得的dispatch() 函数。但是在这里你调用getProduct,它什么都不返回,而是调度动作。 有关useDispatch()钩子的更多详细信息请参阅此处:https://react-redux.js.org/api/hooks#usedispatch

    Redux 在此处有一篇关于如何处理异步逻辑的专门文章:https://redux.js.org/tutorials/fundamentals/part-6-async-logic

    【讨论】:

      猜你喜欢
      • 2020-11-07
      • 2018-02-18
      • 2023-04-06
      • 2019-12-18
      • 1970-01-01
      • 1970-01-01
      • 2018-08-14
      • 1970-01-01
      • 2018-12-27
      相关资源
      最近更新 更多