Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Redux, how to get state in action? [duplicate]

In below example, after I get data from firebase, I want to add user object which is already present in redux store and append it in all the message objects. Request you to help.

Questions:

  1. Is it a good approach to access state in action creator? If yes, how?
  2. If not, what are the alternatives?
db.collection(`messages/${documentId}/chat`)
  .get()
  .then(snapshot => {
    const messages = [];
    snapshot.docs.forEach(doc => {
    console.log("message", doc.id, doc.data());
    messages.push({
      id: doc.id,
      from: doc.data().from,
      to: doc.data().to,   
      text: doc.data().text,
      timestamp: doc.data().timestamp.seconds * 1000
    });
  });
  dispatch({ type: GET_CHAT, payload: messages });
});
like image 693
Vishal Avalani Avatar asked Oct 30 '25 22:10

Vishal Avalani


1 Answers

if your using Redux Thunk middleware, you could simply do that:

const myAction = () => async (dispatch, getState) => {
  const { field1, field2 } = getState().myReducer;
};
like image 65
Hend El-Sahli Avatar answered Nov 03 '25 01:11

Hend El-Sahli