我使用 React 和 Redux 构建了一个购物车,但无法理解最佳实践流程。
我的购物车操作:
export const addToCart = (product) => (dispatch, getState) => {
let { products: cartProducts } = getState().cart;
let { newCartProducts, totalPrice } = handleAddToCart(cartProducts, product);
dispatch(
add({
products: newCartProducts,
total: totalPrice,
})
);
};
Run Code Online (Sandbox Code Playgroud)
模拟服务器处理程序:(更新产品的所有逻辑都在这里 => 我的主要问题是这是否有意义。
export function handleAddToCart(cartProducts, currentProduct) {
let idx = cartProducts.findIndex((p) => p.id === currentProduct.id);
let productInCart = cartProducts[idx];
if (productInCart) {
let updatedProduct = {
...currentProduct,
quantity: productInCart.quantity + 1,
price:
productInCart.price +
applySale({
...currentProduct,
quantity: productInCart.quantity + …Run Code Online (Sandbox Code Playgroud) 假设我有一个Flux包含许多(数十亿个字符串)的输入,如下所示:
这样的字符串有数十亿个,它们无法放入内存中,这就是我想使用反应式方法的原因。
流已排序。现在我想要的是通过前 3 个字符创建一系列有序字符串组:
这Flux最终会出现 HTTP 响应,这意味着所有“app”项目必须在“bib”项目开始之前输出。
如果不使用,Flux我可以使用有序属性并将项目收集到准备好的存储桶中(每个存储桶的字符串数量将适合内存) - 每当前缀发生变化时,我将刷新存储桶并开始收集新的前缀。有序流的一大优点是我知道一旦遇到新的前缀,旧的就不会再出现了。
但使用Flux我不知道如何做到这一点。将会.groupBy()返回Flux,Flux但我认为在尝试将其序列化到 HTTP 响应输出流时这不会起作用。
最初的想法Flux是非常明显的。视图获取事件Actions,然后根据Action存储更新的事件发出事件。然而,当我通读时,redux-toolkit似乎有多种方法可以实现一些本来应该很简单的事情,即从端点获取 JSON 数据:
useEffect()钩子为什么有这么多不同的方法来完成最简单的任务?
谢谢。
我知道也有类似的问题,但是没有一个问题能帮助我解决问题。
所以这是我的问题。
我正在与本机反应并使用通量分配器。我的应用程序分派器的分派和注册工作正常。我的问题是,当我想更改/设置调度寄存器函数中的状态时,总是收到错误消息this.setState()不是函数。当然,我当时以为这一定是一个绑定问题(用es6编写),所以我尝试了各种绑定“ this”,但仍然无法正常工作。有谁知道为什么吗?
这是无效的代码:
testDispatcher() {
AppDispatcher.register( (action) => {
if ( action.action === TEST_ACTION ) {
// I tried setting state inside here
this.setState({
view: action.view
}).bind(this); // with or without this bind doesn't make a difference
// I also tried having a function outside of this function where I set the state.. this doesn't work either.
//this.updateView('home').bind(this);
console.log('dispatch register');
}
});
}
Run Code Online (Sandbox Code Playgroud)
我还尝试在注册函数中管理日志“ this”,并且“ this”确实返回了我的应用程序类。