将类组件函数转换为函数组件函数

沃尔皮塔

我有处理搜索功能的类组件函数。

filterData(offers,searchKey){
const result = offers.filter((offer) =>
        offer.value.toLowerCase().includes(searchKey)||
        offer.expiryDate.toLowerCase().includes(searchKey)||
        offer.quantity.toLowerCase().includes(searchKey)
    )
    this.setState({offers:result})
}

const handleSearchArea = (e) =>{
    const searchKey=e.currentTarget.value;

    axios.get(`/viewPendingSellerOffers`).then(res=>{
        if(res.data.success){
            this.filterData(res.data.existingOffers,searchKey)
        }
    });
}

现在我尝试将这些类组件函数转换为功能组件函数。为此,我尝试了这种方式。

const filterData = (offers,searchKey) => {
const result = offers.filter((offer) =>
        offer.value.toLowerCase().includes(searchKey)||
        offer.expiryDate.toLowerCase().includes(searchKey)||
        offer.quantity.toLowerCase().includes(searchKey)
    )
    setState({offers:result})
}

const handleSearchArea = (e) =>{
    const searchKey=e.currentTarget.value;

    axios.get(`/viewPendingSellerOffers`).then(res=>{
        if(res.data.success){
            filterData(res.data.existingOffers,searchKey)
        }
    });
}

但是我收到一条错误消息,指出“'setState' 未定义”。我该如何解决这个问题?

阿拉哈马迪

解决方案:

import React, {useState} from "react";
import axios from "axios";

const YourFunctionalComponent = (props) => {
  const [offers, setOffers] = useState()

  const filterData = (offersPara, searchKey) => {// I changed the params from offers to offersPara because our state called offers
    const result = offersPara.filter(
      (offer) =>
        offer?.value.toLowerCase().includes(searchKey) ||
        offer?.expiryDate.toLowerCase().includes(searchKey) ||
        offer?.quantity.toLowerCase().includes(searchKey)
    );
    setOffers(result);
  };

  const handleSearchArea = (e) => {
    const searchKey = e.currentTarget.value;

    axios.get(`/viewPendingSellerOffers`).then((res) => {
      if (res?.data?.success) {
        filterData(res?.data?.existingOffers, searchKey);
      }
    });
  };
  return (
    //To use your *offers* state object just call it like this {offers?.El1?.El2}
  );
};
export default YourFunctionalComponent;

注意:建议在访问这样的嵌套对象之前进行空检查res?.data?.existingOffersOptional chaining将在这方面帮助我们。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

将 React 类组件转换为函数组件

将类组件转换为函数组件

将类组件转换为函数组件

将 React 类组件转换为函数组件

无法将类转换为函数组件,“卸载错误”

React 将类转换为函数组件问题

将基于类的组件转换为基于函数的组件失败

React Native:将类组件转换为函数式组件

转换函数组件 -> 类组件

如何将 React 类组件转换为函数组件

如何在reactjs中使用钩子将类组件转换为函数组件

本项目如何将 React 类组件转换为函数组件

如何将类组件转换为函数?(反应)

如何将基于类的组件转换为基于函数的组件?

将类组件转换为功能组件 TypeError:users.filter 不是函数

React - 将 Formik 函数组件迁移到类组件

React - 偏好:函数组件与类组件

在类组件内声明函数与在函数组件内声明

如何将 <any> handleChange 函数从类转换为功能组件

将函数反应组件转换为反应类以添加 ref 属性

NextJS:将 className 函数转换为样式化的组件代码

如何将函数从类组件切换到函数组件

如何将普通脚本转换为在 reactjs 函数组件中使用

函数组件与类组件作为容器组件

将 this.function.bind(this) 替换为函数组件

将类更改为函数组件 antd

在 React Native 中将基于类的组件转换为基于函数的组件时的问题

如何使用 React Hooks 将带有构造函数的类转换为函数式组件?

将箭头函数转换为React组件中的“普通”函数表达式