【问题标题】:How can I add sorting using Redux?如何使用 Redux 添加排序?
【发布时间】:2021-04-30 12:12:31
【问题描述】:

如何为返回 JSON 数组的 API 添加排序?我是 Redux 的新手。我已经安装了redux。谁能告诉我最好的方法是什么?

感谢您的帮助。

import React, { useState, useEffect } from "react";
import Post from "../../Components/Post/Post";
import axios from "axios";

const HomePage = () => {
  const [posts, setPosts] = useState("");

  let config = { Authorization: "............." };
  const url = "..........................";

  useEffect(() => {
    AllPosts();
  }, []);

  const AllPosts = () => {
    axios
      .get(`${url}`, { headers: config })

      .then((response) => {
        const allPosts = response.data.articles;
        console.log(response);
        setPosts(allPosts);
      })
      .catch((error) => console.error(`Error: ${error}`));
  };

  return (
    <div>
      <Post className="Posts" posts={posts} />
    </div>
  );
};

export default HomePage;

【问题讨论】:

    标签: reactjs redux react-hooks


    【解决方案1】:
    1. 这里没有redux。你需要吗?
    2. 如果要对结果进行排序并将排序结果保存到状态:
    ...
     .then((response) => {
            const allPosts = response.data.articles;
            // sort the result here
            const sortedPosts = allPosts.sort((a,b) => 
              // comparer
            );
            setPosts(sortedPosts);
          })
    ...
    
    

    【讨论】:

    • 我必须使用redux来排序。感谢上述解决方案。你能告诉我如何使用redux进行排序吗?我现在已经安装了 redux
    • 你可以排序 -> 使用reducer保存到存储。 redux.js.org/tutorials/fundamentals/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多