【问题标题】:React - Search & filter through a data setReact - 搜索和过滤数据集
【发布时间】:2022-01-21 21:41:43
【问题描述】:

我有一个从快速服务器获取数据的反应应用程序。提取很好,数据显示正确,并且当用户输入与数据集中的某些内容匹配时,搜索功能正常运行。但是,如果用户随后删除了他们的输入,则数据集将不会返回到以前的状态,而是强制用户进行硬刷新。

我将如何解决这个问题,以便当用户删除输入(从“james”到“”)时,页面会过滤回所有数据?


function SearchForm({ data, setData }) {


  
  /*TRACK USER INPUT INTO SEARCH BOX*/
  const [searchValue, setSearchValue] = useState("");

  /*FRONT END: FILTER THROUH DATA TO FIND MATCHES TO SEARCH VALUE*/
  const searchVehicles = (data, searchValue) => {
    const searchedValue = searchValue.toUpperCase();
    const result = data.filter(
      (currenttable) =>
        currenttable.last_name.toUpperCase().includes(searchedValue) ||
        currenttable.first_name.toUpperCase().includes(searchedValue) ||
        currenttable.store.toUpperCase().includes(searchedValue)
    );

    if (searchValue) {
      setData(result);
      console.log(result);
    } else {
      setData(data)
    }
  };

  /*CALL THE FUNCTION WHEN INPUT IS ENETERED*/
  const handleChange = (event) => {
    setSearchValue(event.target.value);
    searchVehicles(data, event.target.value);
    console.log(searchValue)
  };

  /*RESET THE SEARCH BAR AND RENDER ALL DATA */
  const handleReset = () => {
    setSearchValue("");
  };

  /*OUTPUT */

  return (
    <form>
      <label htmlFor="search">Search: </label>
      <input
        className="search-applicant"
        type="text"
        name="search"
        placeholder="Search applicant or store"
        onChange={event => handleChange(event)}
        value={searchValue}
      ></input>
      <button className="search-btn" onClick={handleReset}>
        Refresh
      </button>
    </form>
  );
}

export default SearchForm;

【问题讨论】:

  • 一目了然,为什么不用钩子呢? useEffect(() => { setData(data.filter((currenttable) => currenttable.last_name.toUpperCase().includes(searchedValue) || currenttable.first_name.toUpperCase().includes(searchedValue) || currenttable.store。 toUpperCase().includes(searchedValue))) }, [searchValue]) 你可以完全删除 searchVehicle 函数及其调用编辑:我忘记了 toUpperCase 调用,但你明白了
  • 清理并简化了代码,但并没有解决主要问题。当您删除表单条目时,页面不会重置,它仍保留在过滤后的版本中
  • 当我有机会看看能不能搞清楚的时候,我会尝试做一个快速演示!

标签: reactjs search filter event-handling render


【解决方案1】:

知道了!因此,每次过滤时,您都在修改原始数据。这就是发生的事情。

起初你的组件有data,(例如)现在是["a", "b", "c"]。然后你输入“a”,它会调用setData,将状态更改为[“a”]。但是您正在修改原始数据,所以现在数据中只有“a”,没有其他内容!

你可以改变你的组件而不改变原始数据,直接在你渲染的元素中过滤

function SearchForm({ data }) {


/*TRACK USER INPUT INTO SEARCH BOX*/
  const [searchValue, setSearchValue] = useState("");

  /*CALL THE FUNCTION WHEN INPUT IS ENETERED*/
  const handleChange = (event) => {
    setSearchValue(event.target.value);
  };

  /*RESET THE SEARCH BAR AND RENDER ALL DATA */
  const handleReset = () => {
    setSearchValue("");
  };

  /*OUTPUT */
  return (
    <form>
      <label htmlFor="search">Search: </label>
      <input
        className="search-applicant"
        type="text"
        name="search"
        placeholder="Search applicant or store"
        onChange={event => handleChange(event)}
        value={searchValue}
      />
      {data
        .filter(({ first_name}) => first_name.includes(searchValue)) /* or whatver filter you want*/
        .map(({first_name, last_name, store}) => <div key={store}>{`${first_name} ${last_name} => ${store}`}</div>)
      }
      <button className="search-btn" onClick={handleReset}>
        Refresh
      </button>
    </form>
  );
}

或者您可以创建它的本地副本(例如const [filteredData, setFilteredData] = useState(data),并且您将filteredData 设置为data 并应用过滤器,但您永远不会改变数据)。

See Example Here

【讨论】:

  • 啊,非常感谢!
猜你喜欢
  • 1970-01-01
  • 2017-06-24
  • 1970-01-01
  • 2021-06-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-01
  • 2012-12-04
相关资源
最近更新 更多