[英]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
获取array
或object
,则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.