Passing second argument (ownProps) to mapDispatchToProps - is a bad pactice?












2














I'm new in developing React app. So, i have some questions.
What's the best practice to access state properties inside mapDispatchToProps? Is it a bad practice to use ownProps.store.getState() in mDTP function?
What's the reason to use second argument in mDTP (except to passing additional properties in container components)?



Please advise what to read on this theme.
Thank you very much! Sorry, for my language.










share|improve this question



























    2














    I'm new in developing React app. So, i have some questions.
    What's the best practice to access state properties inside mapDispatchToProps? Is it a bad practice to use ownProps.store.getState() in mDTP function?
    What's the reason to use second argument in mDTP (except to passing additional properties in container components)?



    Please advise what to read on this theme.
    Thank you very much! Sorry, for my language.










    share|improve this question

























      2












      2








      2


      1





      I'm new in developing React app. So, i have some questions.
      What's the best practice to access state properties inside mapDispatchToProps? Is it a bad practice to use ownProps.store.getState() in mDTP function?
      What's the reason to use second argument in mDTP (except to passing additional properties in container components)?



      Please advise what to read on this theme.
      Thank you very much! Sorry, for my language.










      share|improve this question













      I'm new in developing React app. So, i have some questions.
      What's the best practice to access state properties inside mapDispatchToProps? Is it a bad practice to use ownProps.store.getState() in mDTP function?
      What's the reason to use second argument in mDTP (except to passing additional properties in container components)?



      Please advise what to read on this theme.
      Thank you very much! Sorry, for my language.







      reactjs redux






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 23 at 10:08









      Yuriy Piskunov

      946




      946
























          2 Answers
          2






          active

          oldest

          votes


















          0














          This answer might help you:



          What is mapDispatchToProps?



          mapDispatchToProps exists to make the reducers accessible through the component, when following a Container > Component pattern.



          For example, i have these reducers:



          const updateToPreviousMonth = (state) => {
          let newState = state,
          currentMonth = newState.get('currentMonth');
          let previousMonth = sanitizedDate(moment(currentMonth).subtract(1, 'month'));

          return newState.set('currentMonth', previousMonth);
          };

          const updateSelectedDate = (state, action) => {
          return state.set('selectedDate', action.selectedDate);
          };

          export default (state = initialState, action = {}) => {
          switch (action.type) {
          case constants.SET_TO_PREVIOUS_MONTH:
          return updateToPreviousMonth(state);
          case constants.UPDATE_SELECTED_DATE:
          return updateSelectedDate(state, action);
          default:
          return state;
          }
          };


          The constants are the Actions, and the functions (reducers) which return the changed state.



          const mapDispatchToProps = {
          setToPreviousMonth: CalendarViewRedux.actions.setToPreviousMonth,
          updateSelectedDate: CalendarViewRedux.actions.updateSelectedDate
          };

          export class CalendarView extends PureComponent {
          componentDidMount() {
          this.props.loadSchedules();
          }

          render() {
          return (<CalendarViewRender
          {...this.props} />);
          }
          }

          export default connect(mapStateToProps, mapDispatchToProps)(CalendarView);


          On this example, i am passing the actions in mapDispatchToProps, and when called, they will activate the reducers from before, since i used mapDispatchToProps, they are now available in the CalendarView component.



          Hope this helps, please mark as solved if this was helpful.






          share|improve this answer





























            0














            I have 'ownProps' on my mapDispatchToProps to navigate to HomeScreen if I need a reset state (in my case I need to show different screens if user is logged on or off). Here's the example:



            const mapDispatchToProps = (dispatch, ownProps) => {
            return {
            onChangeText: (key, value) => {
            dispatch(onChangeField(key, value))
            },

            goToHomeScreen: () => {
            ownProps.navigation.dispatch(StackActions.reset({index: 0, key: null, actions: [NavigationActions.navigate({ routeName: 'LoggedDrawer'})]}))
            },
            }
            }


            And for calling i simply do this:



            this.props.goToHomeScreen();


            Also, I believe this could help:
            What is the use of the ownProps arg in mapStateToProps and mapDispatchToProps?






            share|improve this answer























              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53444601%2fpassing-second-argument-ownprops-to-mapdispatchtoprops-is-a-bad-pactice%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              0














              This answer might help you:



              What is mapDispatchToProps?



              mapDispatchToProps exists to make the reducers accessible through the component, when following a Container > Component pattern.



              For example, i have these reducers:



              const updateToPreviousMonth = (state) => {
              let newState = state,
              currentMonth = newState.get('currentMonth');
              let previousMonth = sanitizedDate(moment(currentMonth).subtract(1, 'month'));

              return newState.set('currentMonth', previousMonth);
              };

              const updateSelectedDate = (state, action) => {
              return state.set('selectedDate', action.selectedDate);
              };

              export default (state = initialState, action = {}) => {
              switch (action.type) {
              case constants.SET_TO_PREVIOUS_MONTH:
              return updateToPreviousMonth(state);
              case constants.UPDATE_SELECTED_DATE:
              return updateSelectedDate(state, action);
              default:
              return state;
              }
              };


              The constants are the Actions, and the functions (reducers) which return the changed state.



              const mapDispatchToProps = {
              setToPreviousMonth: CalendarViewRedux.actions.setToPreviousMonth,
              updateSelectedDate: CalendarViewRedux.actions.updateSelectedDate
              };

              export class CalendarView extends PureComponent {
              componentDidMount() {
              this.props.loadSchedules();
              }

              render() {
              return (<CalendarViewRender
              {...this.props} />);
              }
              }

              export default connect(mapStateToProps, mapDispatchToProps)(CalendarView);


              On this example, i am passing the actions in mapDispatchToProps, and when called, they will activate the reducers from before, since i used mapDispatchToProps, they are now available in the CalendarView component.



              Hope this helps, please mark as solved if this was helpful.






              share|improve this answer


























                0














                This answer might help you:



                What is mapDispatchToProps?



                mapDispatchToProps exists to make the reducers accessible through the component, when following a Container > Component pattern.



                For example, i have these reducers:



                const updateToPreviousMonth = (state) => {
                let newState = state,
                currentMonth = newState.get('currentMonth');
                let previousMonth = sanitizedDate(moment(currentMonth).subtract(1, 'month'));

                return newState.set('currentMonth', previousMonth);
                };

                const updateSelectedDate = (state, action) => {
                return state.set('selectedDate', action.selectedDate);
                };

                export default (state = initialState, action = {}) => {
                switch (action.type) {
                case constants.SET_TO_PREVIOUS_MONTH:
                return updateToPreviousMonth(state);
                case constants.UPDATE_SELECTED_DATE:
                return updateSelectedDate(state, action);
                default:
                return state;
                }
                };


                The constants are the Actions, and the functions (reducers) which return the changed state.



                const mapDispatchToProps = {
                setToPreviousMonth: CalendarViewRedux.actions.setToPreviousMonth,
                updateSelectedDate: CalendarViewRedux.actions.updateSelectedDate
                };

                export class CalendarView extends PureComponent {
                componentDidMount() {
                this.props.loadSchedules();
                }

                render() {
                return (<CalendarViewRender
                {...this.props} />);
                }
                }

                export default connect(mapStateToProps, mapDispatchToProps)(CalendarView);


                On this example, i am passing the actions in mapDispatchToProps, and when called, they will activate the reducers from before, since i used mapDispatchToProps, they are now available in the CalendarView component.



                Hope this helps, please mark as solved if this was helpful.






                share|improve this answer
























                  0












                  0








                  0






                  This answer might help you:



                  What is mapDispatchToProps?



                  mapDispatchToProps exists to make the reducers accessible through the component, when following a Container > Component pattern.



                  For example, i have these reducers:



                  const updateToPreviousMonth = (state) => {
                  let newState = state,
                  currentMonth = newState.get('currentMonth');
                  let previousMonth = sanitizedDate(moment(currentMonth).subtract(1, 'month'));

                  return newState.set('currentMonth', previousMonth);
                  };

                  const updateSelectedDate = (state, action) => {
                  return state.set('selectedDate', action.selectedDate);
                  };

                  export default (state = initialState, action = {}) => {
                  switch (action.type) {
                  case constants.SET_TO_PREVIOUS_MONTH:
                  return updateToPreviousMonth(state);
                  case constants.UPDATE_SELECTED_DATE:
                  return updateSelectedDate(state, action);
                  default:
                  return state;
                  }
                  };


                  The constants are the Actions, and the functions (reducers) which return the changed state.



                  const mapDispatchToProps = {
                  setToPreviousMonth: CalendarViewRedux.actions.setToPreviousMonth,
                  updateSelectedDate: CalendarViewRedux.actions.updateSelectedDate
                  };

                  export class CalendarView extends PureComponent {
                  componentDidMount() {
                  this.props.loadSchedules();
                  }

                  render() {
                  return (<CalendarViewRender
                  {...this.props} />);
                  }
                  }

                  export default connect(mapStateToProps, mapDispatchToProps)(CalendarView);


                  On this example, i am passing the actions in mapDispatchToProps, and when called, they will activate the reducers from before, since i used mapDispatchToProps, they are now available in the CalendarView component.



                  Hope this helps, please mark as solved if this was helpful.






                  share|improve this answer












                  This answer might help you:



                  What is mapDispatchToProps?



                  mapDispatchToProps exists to make the reducers accessible through the component, when following a Container > Component pattern.



                  For example, i have these reducers:



                  const updateToPreviousMonth = (state) => {
                  let newState = state,
                  currentMonth = newState.get('currentMonth');
                  let previousMonth = sanitizedDate(moment(currentMonth).subtract(1, 'month'));

                  return newState.set('currentMonth', previousMonth);
                  };

                  const updateSelectedDate = (state, action) => {
                  return state.set('selectedDate', action.selectedDate);
                  };

                  export default (state = initialState, action = {}) => {
                  switch (action.type) {
                  case constants.SET_TO_PREVIOUS_MONTH:
                  return updateToPreviousMonth(state);
                  case constants.UPDATE_SELECTED_DATE:
                  return updateSelectedDate(state, action);
                  default:
                  return state;
                  }
                  };


                  The constants are the Actions, and the functions (reducers) which return the changed state.



                  const mapDispatchToProps = {
                  setToPreviousMonth: CalendarViewRedux.actions.setToPreviousMonth,
                  updateSelectedDate: CalendarViewRedux.actions.updateSelectedDate
                  };

                  export class CalendarView extends PureComponent {
                  componentDidMount() {
                  this.props.loadSchedules();
                  }

                  render() {
                  return (<CalendarViewRender
                  {...this.props} />);
                  }
                  }

                  export default connect(mapStateToProps, mapDispatchToProps)(CalendarView);


                  On this example, i am passing the actions in mapDispatchToProps, and when called, they will activate the reducers from before, since i used mapDispatchToProps, they are now available in the CalendarView component.



                  Hope this helps, please mark as solved if this was helpful.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 23 at 10:17









                  Eliâ Melfior

                  8511




                  8511

























                      0














                      I have 'ownProps' on my mapDispatchToProps to navigate to HomeScreen if I need a reset state (in my case I need to show different screens if user is logged on or off). Here's the example:



                      const mapDispatchToProps = (dispatch, ownProps) => {
                      return {
                      onChangeText: (key, value) => {
                      dispatch(onChangeField(key, value))
                      },

                      goToHomeScreen: () => {
                      ownProps.navigation.dispatch(StackActions.reset({index: 0, key: null, actions: [NavigationActions.navigate({ routeName: 'LoggedDrawer'})]}))
                      },
                      }
                      }


                      And for calling i simply do this:



                      this.props.goToHomeScreen();


                      Also, I believe this could help:
                      What is the use of the ownProps arg in mapStateToProps and mapDispatchToProps?






                      share|improve this answer




























                        0














                        I have 'ownProps' on my mapDispatchToProps to navigate to HomeScreen if I need a reset state (in my case I need to show different screens if user is logged on or off). Here's the example:



                        const mapDispatchToProps = (dispatch, ownProps) => {
                        return {
                        onChangeText: (key, value) => {
                        dispatch(onChangeField(key, value))
                        },

                        goToHomeScreen: () => {
                        ownProps.navigation.dispatch(StackActions.reset({index: 0, key: null, actions: [NavigationActions.navigate({ routeName: 'LoggedDrawer'})]}))
                        },
                        }
                        }


                        And for calling i simply do this:



                        this.props.goToHomeScreen();


                        Also, I believe this could help:
                        What is the use of the ownProps arg in mapStateToProps and mapDispatchToProps?






                        share|improve this answer


























                          0












                          0








                          0






                          I have 'ownProps' on my mapDispatchToProps to navigate to HomeScreen if I need a reset state (in my case I need to show different screens if user is logged on or off). Here's the example:



                          const mapDispatchToProps = (dispatch, ownProps) => {
                          return {
                          onChangeText: (key, value) => {
                          dispatch(onChangeField(key, value))
                          },

                          goToHomeScreen: () => {
                          ownProps.navigation.dispatch(StackActions.reset({index: 0, key: null, actions: [NavigationActions.navigate({ routeName: 'LoggedDrawer'})]}))
                          },
                          }
                          }


                          And for calling i simply do this:



                          this.props.goToHomeScreen();


                          Also, I believe this could help:
                          What is the use of the ownProps arg in mapStateToProps and mapDispatchToProps?






                          share|improve this answer














                          I have 'ownProps' on my mapDispatchToProps to navigate to HomeScreen if I need a reset state (in my case I need to show different screens if user is logged on or off). Here's the example:



                          const mapDispatchToProps = (dispatch, ownProps) => {
                          return {
                          onChangeText: (key, value) => {
                          dispatch(onChangeField(key, value))
                          },

                          goToHomeScreen: () => {
                          ownProps.navigation.dispatch(StackActions.reset({index: 0, key: null, actions: [NavigationActions.navigate({ routeName: 'LoggedDrawer'})]}))
                          },
                          }
                          }


                          And for calling i simply do this:



                          this.props.goToHomeScreen();


                          Also, I believe this could help:
                          What is the use of the ownProps arg in mapStateToProps and mapDispatchToProps?







                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Nov 23 at 12:06

























                          answered Nov 23 at 11:06









                          kivul

                          459112




                          459112






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.





                              Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                              Please pay close attention to the following guidance:


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53444601%2fpassing-second-argument-ownprops-to-mapdispatchtoprops-is-a-bad-pactice%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

                              Calculate evaluation metrics using cross_val_predict sklearn

                              Insert data from modal to MySQL (multiple modal on website)