Harshith J.V. Harshith J.V. - 1 year ago 66
Javascript Question

Access props sent to components along with Redux state data

Normally you can access props sent by parent to child on child component. But when redux is used on child components the props sent by parent is lost with use of 'connect' method which maps redux state with components props.


Declaring a component with properties:

<A_Component prop1='1' prop2='2' />

Accessing without redux on child component, works fine:

Same statements will give
error if redux states are used.

Answer Source

Own component props are available as second argument of mapStateToProps function:

// ParentComponent.js

// ... other component methods ...
render() {
  return <TodoContainer id="1" />

// TodoContainer.js

// `ownProps` variable contains own component props
function mapStateToProps(state, ownProps) {
  return {
    todo: state.todos[ownProps.id]