Search code examples
reactjsreduxreact-proptypes

Should we use PropTypes for Redux state?


I have a very simple question for which I have not found any satisfying answer on web so far. Currently I am adding PropTypes in a react application. So, it has redux implemented and I was wondering if I should type check the state coming from redux.

I have a parent component lets say Component1, which renders three different components - something like this -

const Component1 = (props) => {
 return (
  <>
    <Component2 someProps={...} />
    <Component3 someProps={...} />
    <Component4 someProps={...} />
  </>
 )
}

export const mapStateToProps = (state) => {
  return { details: state.details }; //should I use proptypes to check type of details? //
};

export default connect(mapStateToProps)(Component1);

Does it make sense to type check the state coming from redux here? Thanks in advance.


Solution

  • Yes, it makes sense. Because it's props. And no matter where they are from

    const Component1 = (props) => {
     return (
      <>
        <Component2 someProps={...} />
        <Component3 someProps={...} />
        <Component4 someProps={...} />
      </>
     )
    }
    
    Component1.propTypes = {
      details: PropTypes.shape({
        name: string,
        age: number
      })
    }
    
    export const mapStateToProps = (state) => {
      return { details: state.details }; //should I use proptypes to check type of details? //
    };
    
    export default connect(mapStateToProps)(Component1);