简体   繁体   English

如何正确使用,在react-redux应用程序中重新选择

[英]How to use right, reselect in react-redux aplication

I have an application where user can search data depending by his input.我有一个应用程序,用户可以根据他的输入搜索数据。 In my application i try to use reselect.在我的应用程序中,我尝试使用重新选择。


    import React, { useEffect } from "react";
    import { useDispatch, useSelector } from "react-redux";
    import { searchPersonAction } from "./store";

    const Search = () => {
      const dispatch = useDispatch();
      const selector = useSelector((s) => s);
      const search = (e) => {
        const txt = e.target.value;
        dispatch(searchPersonAction(txt));
      };
      return (
        <div>
          <input onChange={search} placeholder="search" />
          <ul>
            {selector.name.map((p) => (
              <li key={p.name}>{p.name}</li>
            ))}
          </ul>
        </div>
      );
    };

    export default Search;

In my store i have an array of persons like this:在我的store ,我有很多这样的人:


    export const persons = [
        {
            name:"jack",
            age: 2
        },
        {
            name:"Jim",
            age: 14
        },
        {
            name:"July",
            age: 92
        },
        {
            name:"Bill",
            age: 1
        },
        {
            name:"Carl",
            age: 72
        },
    ]

Now, when user search something, in the list appears the results according to the name which was searched by the user.现在,当用户搜索某些内容时,列表中会根据用户搜索的名称显示结果。
Question: Is the reselect usefull (protects from to many re-renders) in my case or not?问题:在我的情况下,重新选择是否有用(防止多次重新渲染)? Or using useSelector , in the case above is enought?或者使用useSelector ,在上述情况下就足够了吗?

I don't think reslect here will be necessary.我认为这里没有必要重新考虑。 You can use useMemo to achieve the same result.您可以使用useMemo来实现相同的结果。

    import React, { useEffect, useMemo } from "react";
    import { useDispatch, useSelector } from "react-redux";
    import { searchPersonAction } from "./store";

    const Search = () => {
      const dispatch = useDispatch();
      const persons = useSelector((s) => s.persons);

      const [query, updateQuery] = useState('');
      const searchedPersons = useMemo(() => persons.filter(p => p.name.includes(query)), [query]);

      const search = (e) => {
        updateQuery(e.target.value);
      };
      return (
        <div>
          <input onChange={search} placeholder="search" />
          <ul>
            {searchedPersons.map((p) => (
              <li key={p.name}>{p.name}</li>
            ))}
          </ul>
        </div>
      );
    };

    export default Search;

reselect will be useful if you get array or object from store for example:如果您从store获取arrayobject ,则reselect将很有用,例如:

store店铺

state = {
    persons: [
        {
            name:"Jack",
            age: 2
        },
        {
            name:"Jane",
            age: 14
        },
    ]
}

if you used selector from react-redux and if your typed 'J' in the search field and used searchPersonAction action, then it will change the array of persons in the store , but array stayed the same.如果您使用react-redux中的selector并且如果您在搜索字段中键入'J'并使用searchPersonAction操作,那么它将更改store中的persons数组,但数组保持不变。

state = {
    persons: [
        {
            name:"Jack",
            age: 2
        },
        {
            name:"Jane",
            age: 14
        },
    ]
}

then you receive rerender regardless of whether the data in the array has changed or not.然后无论数组中的数据是否已更改,您都会收到重新渲染。

But if you use reselect and when your typed 'Ja' in the search field, it will be the same array, then you will not get a repeated render, because reselect will memoize data但是,如果您使用reselect ,并且当您在搜索字段中键入'Ja'时,它将是同一个数组,那么您将不会得到重复的渲染,因为reselect会记忆数据

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

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