简体   繁体   English

redux 中单个减速器的加载状态

[英]Loading state for single reducer in redux

I have problem using only one loader state for my reducer in redux.我在 redux 中为我的减速器仅使用一种加载器状态时遇到问题。 The goal is to make sure each different button have 'loading...' before they hit the api, now the problem is all buttons will display 'loading...'目标是确保每个不同的按钮在点击 api 之前都有“正在加载...”,现在的问题是所有按钮都将显示“正在加载...”

I created a demo for this problem https://codesandbox.io/s/jlq103oq1v我为这个问题创建了一个演示https://codesandbox.io/s/jlq103oq1v

You need to separate loading states, Currently you only have one loading state and you take your decision based on that, but you buttons need to identify if the loading sstate is meant for them or someone else.您需要分离加载状态,目前您只有一种加载状态,您可以基于此做出决定,但是您的按钮需要确定加载状态是针对他们还是其他人。 You can either renaming your loading states to approve_loading and reset_loading and use them like您可以将加载状态重命名为approve_loading加载和reset_loading加载并reset_loading使用它们一样使用它们

@connect(state => state.items, { approveItem, resetItem })
export default class Items extends Component {
  render() {
    return (
      <div>
        <div>status: {this.props.item.status}</div>
        <button onClick={() => this.props.resetItem()}>
          {this.props.reset_loading ? "loading..." : "Reset"}
        </button>
        <button onClick={() => this.props.approveItem()}>
          {this.props.approve_loading ? "loading..." : "Approve"}
        </button>
      </div>
    );
  }
}

working codesanbox工作密码箱

or structure your reducer differently,或以不同的方式构造您的减速机,

working sandbox for the same相同的工作沙箱

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

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