【问题标题】:How to implement fifo queue with Reactjs&Redux如何使用 Reactjs&Redux 实现 fifo 队列
【发布时间】:2020-03-09 16:36:21
【问题描述】:

您好,我需要一些关于 react-redux 的帮助,因为我还在学习,假设我正在尝试制作简单的队列管理,并且我有这个 reducer:

const initialState = {
    currentQueue = {},
    queues=[],
    loading: false
}

数据会是这样的:

currentQueue: 
{{'id_queue': '1', 'queue_no': 'A001', 'status': 0}}

queues:
[0:{'id_queue': 1, 'queue_no': 'A001', 'status': 0 },
 1:{'id_queue': 2, 'queue_no': 'A002', 'status': 0 }]

如何从 queues 数组中获取单个对象到 currentQueue ?像下一个队列一样,我只知道通过 id 获取对象(如人员资料)。所以我需要一个一个地渲染队列数组列表以显示当前队列号,或者我应该只将 mysql 查询限制操作 1 吗?。

如果有更好的方法,请赐教我用mysql在react-redux中实现正确的队列,谢谢。因为我已经尝试使用 react-redux 搜索一些队列实现但没有运气。

【问题讨论】:

  • 这能回答你的问题吗? Correct modification of state arrays in ReactJS
  • 虽然欺骗目标解决了组件状态的更新,但它与 Redux 的不可变状态使用的逻辑相同。
  • 您的标题 fifo(先进先出)意味着从您的队列中删除一个项目。你想简单地访问你的 redux 状态还是删除(先出)项目? currentQueue 是队列还是队列持有的对象?

标签: javascript reactjs redux


【解决方案1】:

javascript 数组可以像 fifo queue 一样工作。

const fifo = [];
fifo.push(1)
fifo.push(2)
console.log(fifo.push(3)) // undefined
console.log(fifo) // [1, 2, 3]
const val = fifo.shift()
console.log(val, fifo) // 1, [2, 3]

然而 push、pop、unshift 和 shift 都会改变数组。这是immutable way。

function immutablePush(arr, newEntry){
  return [ ...arr, newEntry ]      
}

function immutableShift(arr){
  return arr.slice(1)     
}

const fifo = [];
immutablePush(fifo, 1) // returns [1]
immutablePush(fifo, 2) // [1, 2]
immutablePush(fifo, 3) // [1, 2, 3] 
const val = fifo[0] // 1
immutalbeShift(fifo) // returns [2, 3]

如果您想像在对象中一样查找数据,则需要normalize data。

在大多数情况下,您可以简单地使用findIndex

const findByIdQueue(array, id) => {
  const i = array.findIndex(item => item.id_queue === id);
  // if i = -1 (not found) return undefined else return found item
  return ~i ? undefined : array[i];
}

在 react redux 中,我们希望将访问代码和更新代码分开。我们使用选择器访问:

const selectFirstItem = state => {
  // access state.fifo but set fifo to [] if state or state.fifo are undefined
  const { fifo = [] } = state || {};
  // return first array item or undefined if there is an empty list
  return fifo[0]; 
}
const selectItemById = (state, ownProp) => {
  const { fifo = [] } = state || {};
  const { id } = ownProp || {};
  return findByIdQueue(fifo, id);
}
const mapStateToProps = (state, ownProp) => ({
  firstItem = selectFirstItem(state);
  itemById = select(state, ownProp)  // expect parent of MyCoolControl to pass in prop id
// <MyCoolControl id={24} />
})

export default connect(mapStateToProps)(MyCoolControl)

我们用行动更新:

const addItem = item => ({type: 'ADD_ITEM', item})
const removeFirstItem = () => ({type: 'REMOVE_FIRST_ITEM'})

const fifoReducer = (prev = defaultValue, action = {}) => {
  const { type, item} = action;
  switch (type) {
    case "ADD_ITEM":
      return [...prev, item];
    case "REMOVE_FIRST_ITEM":
      return prev.slice(1);
    default: {
      return prev;
    }
  }
};

【讨论】:

  • 这真的帮助我了解它是如何工作的,非常感谢先生!
【解决方案2】:

可以使用普通js数组的push和shift方法:

let x=[];
x.push(1);
x.push(2);
x.push(3);
x.push(4);
x.push(5);
console.log(x.shift());
console.log(x.shift());
console.log(x.shift());
console.log(x);
console.log(x.shift());
console.log(x.shift());

输出:

1
2
3
[4,5]
4
5

【讨论】:

猜你喜欢
  • 2023-04-03
  • 2014-08-24
  • 2012-04-30
  • 1970-01-01
  • 2017-04-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-18
相关资源
最近更新 更多