【问题标题】:How do I return my original list on the 3rd click?如何在第三次点击时返回我的原始列表?
【发布时间】:2021-08-24 06:02:30
【问题描述】:

前端代码

  const [customerList, setCustomerList] = useState([]); //store all that information of the database in a list
  //make an axios request to get information from database
  useEffect(() => {
    Axios.get("http://localhost:3001/customers").then((response) => {
      setCustomerList(response.data);
    });
  }, []);
//sort function final
  const [counter, setCounter] = useState(0);
  const [sortedCustomerList, setSortedCustomerList] = useState([]);

  function sortCustomer() {
    const sortedCustomer = [...customerList];
    let sortCount = counter;
    //check the current sortCount, if it is 3 then go back to 1, if not then increase by 1
    if (sortCount === 3) {
      sortCount = 1;
      setCounter(1);
    } else {
      sortCount += 1;
      setCounter(sortCount);
    }
    console.log(sortCount);
    if (sortCount < 3) {
      sortedCustomer.sort(function (x, y) {
        if (sortCount === 1) {
          return x.contacted === y.contacted
            ? 0
            : x.contacted === "Yes"
            ? -1
            : 1;
        } else if (sortCount === 2) {
          return x.contacted === y.contacted
            ? 0
            : x.contacted === "Yes"
            ? 1
            : -1;
        }
      });

      setCustomerList(sortedCustomer);
    } else {
      setCustomerList(customerList);
    }
  }
<th onClick={() => sortCustomer()}>Contacted?</th>

我已经实现了一个排序函数 onClick 方法。所以第一次点击返回降序列表,第二次点击返回升序列表,但第三次点击不返回原来的customerList。如何解决这个问题。

【问题讨论】:

    标签: javascript mysql reactjs frontend backend


    【解决方案1】:
    const [customerList, setCustomerList] = useState([]); //store all that information of the database in a list
    const [counter, setCounter] = useState(0);
    const [sortedCustomerList, setSortedCustomerList] = useState([]);
    useEffect(() => {
        Axios.get("http://localhost:3001/customers").then((response) => {
            setCustomerList(response.data);
            setSortedCustomerList(response.data);
        });
    }, []);
    
    function sortCustomer() {
        const sortedCustomer = [...customerList];
        let sortCount = counter;
        if (sortCount === 3) {
            sortCount = 1;
            setCounter(1);
        } else {
            sortCount += 1;
            setCounter(sortCount);
        }
    
        if (sortCount < 3) {
            sortedCustomer.sort(function (x, y) {
            if (sortCount === 1) {
                return x.contacted === y.contacted
                ? 0
                : x.contacted === "Yes"
                ? -1
                : 1;
            } else if (sortCount === 2) {
                return x.contacted === y.contacted
                ? 0
                : x.contacted === "Yes"
                ? 1
                : -1;
            }
            });
            setSortedCustomerList(sortedCustomer);
        } else {
            setSortedCustomerList(customerList);
        }
    }
    
    Use sortedCustomerList to print on the screen
    
    <th onClick={() => sortCustomer()}>Contacted?</th>
    

    【讨论】:

    • 你从哪里得到 customerList?
    • 我编辑了我的代码以显示我在哪里获得 customerList
    • 尝试改回if (sortCount &lt; 3) 你改成代码
    • 您好,我改回来了,但我控制台记录的第三次点击仍然没有返回原始客户列表
    • ` setCustomerList(sortedCustomer); } 其他 { setCustomerList(customerList); }` 将 setCustomerList 更改为 setSortedCustomerList 并且不要在问题中更新,在您的本地尝试这个
    猜你喜欢
    • 1970-01-01
    • 2017-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多