简体   繁体   English

React Show data with page refresh on Button click

[英]React Show data with page refresh on Button click

I have array of object as data.我有 object 数组作为数据。 The data is displayed on initial page load.数据在初始页面加载时显示。 When Clear Book button is called with clearBooks() function, I set array to empty (No Data is displayed) and change button value to Show Books当使用 clearBooks() function 调用 Clear Book 按钮时,我将数组设置为空(不显示数据)并将按钮值更改为Show Books

What I am trying to achieve is when Show Books button is clicked I would like to show all objects as before.我想要实现的是,当单击Show Books按钮时,我想像以前一样显示所有对象。 I though of refreshing page with window.location.reload();我想用window.location.reload();刷新页面when Show Books is clicked (If there are better solution, open to use them).单击Show Books时(如果有更好的解决方案,请打开以使用它们)。 I need help to achieve this functionality in code我需要帮助才能在代码中实现此功能

main.js main.js


const clearBooks = () => {
    if (buttonTitle === "Clear Books") {
      setButtonTitle("Show Books");
    }
    setBooksData([]);
  };
return (
    <section className="booklist">
      {booksData.map((book, index) => {
        return (
          <Book key={index} {...book}>
          </Book>
        );
      })}
      <div>
        <button type="button" onClick={clearBooks}>
          {buttonTitle}
        </button>
      </div>
    </section>
  );

Data数据

export const books = [
  {
    id: 1,
    img: "https://m.media/_QL65_.jpg",
    author: " A ",
    title: "B",
    
  },
{
    id: 2,
    img: "https://m.media/_QL65_.jpg",
    author: " A ",
    title: "B",
    
  },

You could store the originally fetched data into a separate state and reset the booksData array by setting its value equal to that state on the Show Books click:您可以将最初获取的数据存储到单独的 state 中,并通过在Show Books单击上将其值设置为等于 state 的值来重置booksData数组:

const [originalBooksData, setOriginalBooksData] = useState([]);
const [booksData, setBooksData] = useState([]);

const fetchBooks = async () => {
    ...
    // After booksData have been fetched set originalBooksData equal to it
    setOriginalBooksData(booksData)
}

const clearBooks = () => {
  if (buttonTitle === 'Clear Books') {
    // Clear the books
    setButtonTitle('Show Books');
    setBooksData([]);
  } else {
    // Reset the books
    setBooksData(originalBooksData);
  }
};

...

Of course, you will need to set the originalBooksData equal to booksData when the data are loaded.当然,您需要在加载数据时将originalBooksData设置为等于booksData
This will prevent the need to refresh the page when clearing the data.这将避免在清除数据时需要刷新页面。

You can achieve this by using useState hook.您可以通过使用useState挂钩来实现此目的。

const books = [
  {
    id: 1,
    img: "https://m.media/_QL65_.jpg",
    author: "A",
    title: "B",
  },
  {
    id: 2,
    img: "https://m.media/_QL65_.jpg",
    author: " A ",
    title: "B",
  },
]

const [bookList, setBookList] = useState(books);

const clearBooks = () => {
  setBookList("");
}

const showBooks = () => {
  setBookList(books);
}

Set bookList to empty when you need to clear book list and to books object when yoou want to show your list books.当您需要清除图书列表时将 bookList 设置为空,当您想要显示您的图书列表时将 bookList 设置为books object。

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

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