Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 在redux中集成重新选择_Reactjs - Fatal编程技术网

Reactjs 在redux中集成重新选择

Reactjs 在redux中集成重新选择,reactjs,Reactjs,我有一个简单的搜索应用程序,根据用户输入,它在前端得到结果。 我的redux代码: 从“/persons”导入{persons}; 从“redux”导入{createStore}; //康坦特 导出常量搜索={ 搜索人:“搜索人” }; //行动 导出const searchPersonAction=(person)=>{ const personsearch=persons.filter((p)=> p、 name.toLowerCase().includes(person.toLower

我有一个简单的搜索应用程序,根据用户输入,它在前端得到结果。
我的redux代码:

从“/persons”导入{persons};
从“redux”导入{createStore};
//康坦特
导出常量搜索={
搜索人:“搜索人”
};
//行动
导出const searchPersonAction=(person)=>{
const personsearch=persons.filter((p)=>
p、 name.toLowerCase().includes(person.toLowerCase())
);
返回{
类型:SEARCH.SEARCH\u PERSON,
有效载荷:人员搜索
};
};
//减速器
常量初始状态={
姓名:人
};
export const search=(state=initialState,{type,payload})=>{
开关(类型){
case SEARCH.SEARCH\u人员:
返回{
……国家,
名称:有效载荷
};
违约:
返回状态;
}
};
//贮藏

导出常量存储=创建存储(搜索)您可以将
使用选择器
中的函数从
重新选择
包装到
创建选择器
中,这将记忆选择器值。您可以这样做:

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

const memoiseSelector = createSelector(
  (s) => s.name,
  (name) => name
);

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

export default Search;
import React,{useffect}来自“React”;
从“重新选择”导入{createSelector};
从“react redux”导入{useDispatch,useSelector};
从“/store”导入{searchPersonAction};
常量memoiseSelector=createSelector(
(s) =>s.name,
(名称)=>名称
);
常量搜索=()=>{
const dispatch=usedpatch();
const name=useSelector(memoiseSelector);
常量搜索=(e)=>{
const txt=e.target.value;
调度(searchPersonAction(txt));
};
返回(
    {name?.map((p)=>(
  • {p.name}
  • ))}
); }; 导出默认搜索;

您可以在官方网站上查看更多示例。

我更改了,但它不起作用。您能帮我吗?我已经更新了答案,我错过了createSelector上的第二个参数。您可以找到更新的实现。我还建议不要根据静态文件中的persons对象进行修改,而是根据state中的值进行修改。这应该是唯一的真相来源。memoiseSelector只是一个使用useSelector传递状态的函数。如果您选择console.log(memoiseSelector)。你会把它看作是它自己的功能。