[英]ngrx - where to make web requests to update store after successful action
I'm currently working with ngrx and nrg-entity.我目前正在使用ngrx 和nrg-entity。 I wonder what the best practices are for updating the store after a successful web request if the response doesn't contain the entity object that have to be stored.
如果响应不包含必须存储的实体 object ,我想知道在成功的 web 请求后更新商店的最佳做法是什么。
Example:例子:
// My state
export interface State extends EntityState<Book> {}
I make a web request to create a book.我提出了 web 请求来创建一本书。 From backend I only get an id of the created book.
从后端我只能得到创建书的 id。 By default you create a reducer to put that new created book in your store like so:
默认情况下,您创建一个 reducer 将新创建的书放在您的商店中,如下所示:
createReducer(
initialState,
on(BookActions.createBookSuccess, (state, { book}) =>
bookAdapter.addOne(book, { ...state, creating: false })
));
I see two options to return the new book to the reducer:我看到有两个选项可以将新书还给减速器:
Is one of these options the way to move?这些选项之一是移动的方式吗? Or is there a better way?
或者,还有更好的方法?
I hope I have been able to make my problem clear.我希望我能够把我的问题说清楚。
There are a lot of ways you can do this but my instinct would be to have the effect trigger another action/effect that gets and sets the next value.有很多方法可以做到这一点,但我的直觉是让效果触发另一个获取和设置下一个值的动作/效果。 So your first effect returns something like this,
所以你的第一个效果返回这样的东西,
return [
..., //Whatever it already returns
LoadBookByIdEffect
]
Then your LoadBookIdEffect triggers an Action that sets the book value in state,然后您的 LoadBookIdEffect 会触发一个 Action 来设置 state 中的账面价值,
return SetBook({ book });
Here is a more detailed explanation of the syntax for chaining effects together like this,这是对像这样将效果链接在一起的语法的更详细说明,
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.