Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ownProps is missing props added through mapStateToProps

I have the following form:

let NoteForm = ({handleSave}) => {
  return (
    <form onSubmit={handleSave}>
      <Field component="input" type="text" name="body" />
      <button type="submit">Submit</button>
    </form>
  )
}

And:

const mapStateToProps = (state, ownProps) => {
  return {
    isNew: state.isNew
  }
}

And finally:

const mapDispatchToProps = (dispatch, ownProps) => {
  return {
    handleSave: () => {
      dispatch(saveNote(ownProps)) // ownProps does not contain "isNew" prop
    }
  }
}

The prop isNew is not included in ownProps. I hope it's an obvious mistake I am making here and unable to see it.

The component that renders NoteForm is Note and renders it conditionally as such:

{ (isNew || isEdit) && <NoteForm /> }

I can certainly pass the props in as attributes, but at this point I am stubborn as I am unable to understand why my prop is missing. I have already tried passing it in as an argument to my component as such: {handleSave, isNew} but that doesn't work either.

For reference, my connect is:

NoteForm = connect(
  mapStateToProps,
  mapDispatchToProps
)(NoteForm);
like image 798
dipole_moment Avatar asked Nov 29 '16 14:11

dipole_moment


People also ask

What is ownProps in mapStateToProps?

The mapStateToProps(state, ownProps) is specified as the first argument of connect() call and its ownProps parameter receives the props object of the wrapper component. If the ownProps parameter is not present, React Redux skips calling the function at the props change.

What is the difference between mapStateToProps () and mapDispatchToProps ()?

mapStateToProps() is a function used to provide the store data to your component. On the other hand, mapDispatchToProps() is used to provide the action creators as props to your component.

Is useSelector the same as mapStateToProps?

useSelector and useDispatch are a set of hooks to use as alternatives to the existing connect() higher-order component. The equivalent of map state to props is useSelector. It takes in a function argument that returns the part of the state that you want. The equivalent of map dispatch to props is useDispatch.

What do mapStateToProps and mapDispatchToProps actually do?

The mapStateToProps and mapDispatchToProps deals with your Redux store's state and dispatch , respectively. state and dispatch will be supplied to your mapStateToProps or mapDispatchToProps functions as the first argument.


1 Answers

Currently I can't test this, but it makes sense that ownProps are the properties that are passed to the component that is being returned by connect(...)(YourComponent). On the other hand isNew is passed directly to YourComponent by mapStateToProps, so you wouldn't have access to it in the mapDispatchToProps callback.

Somewhere in your app...

const ReturnedComponent = connect(...)(YourComponent)

render () {

  <ReturnedComponent {...ownProps} />
}

These ownProps are accessible by mapStateToProps, but not the ones passed by mapDispatchToProps.


See a possible solution to your problem here recommended by the creator of redux himself.

like image 111
Lyubomir Avatar answered Sep 17 '22 14:09

Lyubomir