The course is part of this learning path
This module looks at static versions in React, Props, and Mock Components to use when testing an application.
The objectives of this module are to provide you with an understanding of:
- How to be able to build a static version of an application
- How to use Props in components
- How to be able to test Component snapshots and Components with props
- How to be able to Mock components when testing
This learning path is aimed at all who wish to learn how to use the ReactJS framework.
Prerequisites of the Course
We welcome all feedback and suggestions - please contact us at firstname.lastname@example.org to let us know what you think.
If we are making components that are to be re-used, perhaps across several applications and by several different developers, it's useful if it's clear what a component should receive as props. The PropTypes package, which needs to be imported into any component that wants to use proptypes, supplies this functionality. It helps ensure the correct usage of components by clearly defining what data type a prop should be, and can be even more explicit by specifying the shape of an object passed as a prop. You can see in the code, after adding the import, we specify proptypes for each props in this component. This is done by adding a prop types object to the component, and then specifying the name of the prop and the proptype it should be. So for header text, this is a proptype.string. For the object prop, we have specified its shape and you can see that for the function prop, we are able to specify it's prop type as func. Console warnings are produced if the correct type for a prop was not used, as you can see from the first warning on the browser window. Proptypes can also be used to express if a particular prop has to be supplied or not. We can chain the, dot is required to any prop type declaration, like has been done for the unsupplied proptype of any. This has the effect of producing a console warning if the prop is not available and you can see this in the second warning, in the console window. Overall, proptypes can make components easier to read and understand. Some argue that, used correctly, proptypes can negate the need for typescript when writing React applications. If you start your project using create-react-app, the proptypes package is included in the project by default so you don't need to download and install it. However, it will only be bundled into your deployment app if you actually use it. If you didn't use create-react-app to start your project, proptypes is an MPM package that can be easily added. More detailed information on proptypes can be found in the official React documentation. So now you have all the knowledge you need to be able to create a fully static version of an application.