简单计算不接受输入

发布于 2025-01-16 04:57:21 字数 654 浏览 0 评论 0原文

Redux 库 easy-peasy 有一个名为 compulated 的函数,它是标准 Redux 选择器的替代方案:

import { computed } from 'easy-peasy'

const model = {
  session: {
    totalPrice: computed(state => state.price + state.tax)
  }
}

然后在组件中调用选择器,如下所示:

import { useStoreState } from 'easy-peasy'

function TotalPriceOfProducts() {
  const totalPrice = useStoreState(state => state.products.totalPrice)
  return <div>Total: {totalPrice}</div>
}

问题是,似乎没有将输入传递给选择器的方法。如果我需要该状态的数组中的特定对象,我无法将该对象的 ID 作为输入传递。我唯一的选择是在组件方面执行此操作。 Redux 选择器的优点是函数,因此我可以传递要在选择器逻辑中使用的输入。

有人使用 easy-peasy 之前遇到过这个问题吗?

The Redux library easy-peasy has a function called computed which is an alternative to standard Redux selectors:

import { computed } from 'easy-peasy'

const model = {
  session: {
    totalPrice: computed(state => state.price + state.tax)
  }
}

And then the selector is called in the component like this:

import { useStoreState } from 'easy-peasy'

function TotalPriceOfProducts() {
  const totalPrice = useStoreState(state => state.products.totalPrice)
  return <div>Total: {totalPrice}</div>
}

The problem is that there doesn't seem to be a way to pass inputs to the selector. If I need a specific object in an array in the state, I can't pass the ID of the object as an input. My only option is to do this on the component side. Redux selectors have the advantage of being functions, so I can pass inputs to be used in the selector logic.

Anyone use easy-peasy come across this problem before?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

满意归宿 2025-01-23 04:57:21

我无法将对象的 ID 作为输入传递

要启用此功能,您必须返回 作为您的计算属性

这是一个示例,公开了一个计算函数 getTodoById

import { computed } from 'easy-peasy'

const model = {
todos: [],
getTodoById: computed(state => {
// Note how we are returning a function instead of state
//

I can't pass the ID of the object as an input

To enable this, you have to return a function as your computed property.

Here is an example, exposing a computed function getTodoById:

import { computed } from 'easy-peasy'

const model = {
  todos: [],
  getTodoById: computed(state => {
    // Note how we are returning a function instead of state
    //      ????
    return (id) => state.todos.find(t => t.id === id)
  })
}

Then you can use it like this:

import { useStoreState } from 'easy-peasy'

function Todo({ id }) {
  const getTodoById = useStoreState(state => state.getTodoById)
  const matchingTodo = getTodoById(id);
  // ... etc
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文