简体   繁体   English

如何使用 redux saga 从 redux 商店获取项目:已解决

[英]How to get an item from redux store with redux saga: SOLVED

EDIT: solution at the end编辑:最后的解决方案

So I have fetched the data from an API and stored it into my Redux store.所以我从 API 获取数据并将其存储到我的 Redux 存储中。 It's an array of objects, containing 100ish objects.它是一个对象数组,包含 100 个对象。 On the front end I am displaying 4 keys from every object.在前端,我显示每个对象的 4 个键。

What I am trying to do now is on the front end, search by a key (name or year) and render the results.我现在要做的是在前端,按键(名称或年份)搜索并呈现结果。 Ideally, it would be something like ILIKE in PSQL, but even having the result only when e.target.value === obj.super.nested.key||obj.super.nested.key_two would be amazing.理想情况下,它会像 PSQL 中的 ILIKE 一样,但即使只有在 e.target.value === obj.super.nested.key||obj.super.nested.key_two 时才有结果也会很棒。

I have tried using the select() from redux-saga/effects to no avail.我尝试使用 redux-saga/effects 中的 select() 无济于事。

What I tried originally is on onChange of the input field, to dispatch an action to a watcher with the e.target.value as a parameter, which would call a function using that the value as a parameter for select().我最初尝试的是在输入字段的 onChange 上,以 e.target.value 作为参数将动作分派给观察者,该观察者将使用该值作为 select() 的参数调用函数。

The code with PSQL instead of redux saga would be :使用 PSQL 而不是 redux saga 的代码将是:

    const [val, setVal] = useState("");
---------------------------------------------
    useEffect(() => {
        if (val.length >= 3) {
            axios.get(`/searchCards/${val}.json`).then(results => {});
        } else if (val.length <= 3) {
            setCard("");
        }
    }, [val]);
---------------------------------------------
 function handleChange(e) {
    e.preventDefault();
    setVal(e.target.value);
  }

----------------------------------------------
   <form noValidate onSubmit={e => this.submit(e)}>
            <input
              noValidate
              name="searchValue"
              placeholder="name"
              onChange={e => handleChange(e)}
            />
            <br />
            <br />
            <input onChange={e => handleChange(e)} type="date" />
          </form>

node.js节点.js

app.get("/searchCards/:str.json", (req, res) => {
    db.findCard(req.params.str)
        .then(results => {
            let result = results.rows[0].name;
            res.json({ result });
        })
        .catch(err => {
            console.log("error in searching for cards by name: ", err.message);
            res.json(null);
        });
});

exports.findCard = function findCard(str) {
    return db.query(`SELECT * FROM cards WHERE name ILIKE $1 LIMIT 1`, [
        str + "%"
    ]);
};

How could I achieve the same with redux-saga, searching the store by value, and returning the whole object which contains that value?我怎么能用 redux-saga 实现同样的效果,按值搜索存储,并返回包含该值的整个对象?


SOLUTION解决方案

I have found my workaround if anyone wants to know.如果有人想知道,我已经找到了我的解决方法。 The documentation on redux-saga is not very rich, so I did it the only way I knew how. redux-saga 的文档不是很丰富,所以我用我知道的唯一方法来做了。

Basically, when the component mounts, I request the data from the api and store in the redux store.基本上,当组件挂载时,我从 api 请求数据并存储在 redux 存储中。 As soon as it's in the store, I render it on the FE.只要它在商店里,我就会在 FE 上渲染它。

Since on the change of an input field I want to display only some parts of the previously displayed data, I made a ternary which says if the input field value exists/changes, map through the store and return where input value.toLowerCase().startsWithstartsWith(value_in_the_store), else, return all the data.由于在更改输入字段时,我只想显示先前显示数据的某些部分,因此我创建了一个三元组,表示输入字段值是否存在/更改,通过存储映射并返回输入 value.toLowerCase() 的位置。 startsWithstartsWith(value_in_the_store),否则返回所有数据。

It works like a charm.它就像一个魅力。

If anyone has another idea, I'd love to hear.如果有人有其他想法,我很乐意听到。

SOLUTION解决方案

I have found my workaround if anyone wants to know.如果有人想知道,我已经找到了我的解决方法。 The documentation on redux-saga is not very rich, so I did it the only way I knew how. redux-saga 的文档不是很丰富,所以我用我知道的唯一方法来做了。

Basically, when the component mounts, I request the data from the api and store in the redux store.基本上,当组件挂载时,我从 api 请求数据并存储在 redux 存储中。 As soon as it's in the store, I render it on the FE.只要它在商店里,我就会在 FE 上渲染它。

Since on the change of an input field I want to display only some parts of the previously displayed data, I made a ternary which says if the input field value exists/changes, map through the store and return where input value.toLowerCase().startsWithstartsWith(value_in_the_store), else, return all the data.由于在更改输入字段时,我只想显示先前显示数据的某些部分,因此我创建了一个三元组,表示输入字段值是否存在/更改,通过存储映射并返回输入 value.toLowerCase() 的位置。 startsWithstartsWith(value_in_the_store),否则返回所有数据。

It works like a charm.它就像一个魅力。

If anyone has another idea, I'd love to hear.如果有人有其他想法,我很乐意听到。

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

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