javascript - React redux favorites action - Stack Overflow

I am new to react-redux world and having some trouble visualising a piece of plex data flow (I think).A

I am new to react-redux world and having some trouble visualising a piece of plex data flow (I think).

Assume the state contains a collection of tracks and an array of favorite track ids. User could favorite a track from a number of various ponents e.g. musicplayer, tracklist, charts and all the others would have to rerender.

At the moment, I'm triggering an action to add/remove the track id to/from the favorites array. But I can't quite see how to proceed from there.

My plan is to trigger another action for e.g. the trackItem reducer to listen and carry on. Or could each related ponent directly subscribe to changes of the favorites collection? Or can I have two reducers listening to the same action? I have now idea how to implement something like that and also I have a gut feeling that I'm on the wrong path.

Feels like I'm struggling to get rid of my backbone-marionette habits. How would you do it?

My other plan is to have an isFavorited boolean within the track item json and use an action/reduces to update/toggle that property. I understand that normalizr will merge instances with the same id, so any ponent subscribed to its changes will react.

I am new to react-redux world and having some trouble visualising a piece of plex data flow (I think).

Assume the state contains a collection of tracks and an array of favorite track ids. User could favorite a track from a number of various ponents e.g. musicplayer, tracklist, charts and all the others would have to rerender.

At the moment, I'm triggering an action to add/remove the track id to/from the favorites array. But I can't quite see how to proceed from there.

My plan is to trigger another action for e.g. the trackItem reducer to listen and carry on. Or could each related ponent directly subscribe to changes of the favorites collection? Or can I have two reducers listening to the same action? I have now idea how to implement something like that and also I have a gut feeling that I'm on the wrong path.

Feels like I'm struggling to get rid of my backbone-marionette habits. How would you do it?

My other plan is to have an isFavorited boolean within the track item json and use an action/reduces to update/toggle that property. I understand that normalizr will merge instances with the same id, so any ponent subscribed to its changes will react.

Share Improve this question edited Mar 7, 2017 at 23:18 Robin 7,9042 gold badges34 silver badges49 bronze badges asked Oct 7, 2015 at 23:08 ap-oap-o 1701 silver badge8 bronze badges 0
Add a ment  | 

3 Answers 3

Reset to default 2

Or could each related ponent directly subscribe to changes of the favorites collection

They could. But do these ponents all share some parent ponent? If so I would have that parent ponent subscribe to the state change of the favorites array, and pass that down as props to the ponents that need it.

I would remend really reading through the redux docs: https://rackt.github.io/redux/

Especially usage with React: https://rackt.github.io/redux/docs/basics/UsageWithReact.html

Typically you would have a 'smart' ponent that renders for a route, and that would subscribe to the redux store and pass down the data its nested 'dumb' ponents need.

So have your smart ponent(s) subscribe to the state change of the favorites array and pass it down as a prop to the ponents that need it.

It's all right to listen to one action in more than one reducer, so maybe go down that route?

Do your ponents share mon parent ponent? If they do, connect it to your redux app state and pass favorite ids array down to each one; then dispatch action addFav or removeFav from any ponent, react in favorites reducer and see redux passing new props to react ponents.

I think you should first understand about smart and dumb ponents in reactjs, here is the link, so you will be having a single smart which connects to you reducer and updates your child(dumb) ponent.

If you still wants to have two reducers, you can have a action which executes its operation as a result it calls another action. to achieve this you need to have a redux-async-transitions, the example code is given below

function action1() {
 return {
  type: types.SOMEFUNCTION,
  payload: {
   data: somedata
  },
  meta: {
   transition: () => ({
    func: () => {
     return action2;
    },
    path : '/somePath',
    query: {
     someKey: 'someQuery'
    },
    state: {
     stateObject: stateData
    }
   })
  }
 }
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745095527a4610953.html

相关推荐

  • javascript - React redux favorites action - Stack Overflow

    I am new to react-redux world and having some trouble visualising a piece of plex data flow (I think).A

    12小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信