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 reused, perhaps across several applications and by several different developers. It's useful if it's clear what a component you should receive as props. The prop types package which needs to be imported into any component that wants to use prototypes, supplies this functionality. That helps ensure the correct usage of components by clearly defining what data type of prop should be and could 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 prop types 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 prop type it should be.
So for head attacks, this is a prop type.string, for the object prop, we have specified its shape, and you can see that for the function prop, we are able to specify its prop type as funk. 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. Prop types 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 on supplied prop type 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 prop types can make components easier to read and understand, some argue that used correctly prop types can negate the need for TypeScript when writing applications if you start your project using create react up, the prop types 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 prop types is a npm package that can be easily added. More detailed information on prop types can be found in your official react documentation. So now you have all the knowledge you need to be able to create a fully static version of an application.
Ed is an Outstanding Trainer in Software Development, with a passion for technology and its uses and holding more than 10 years’ experience.
Ed is responsible for delivering QA’s Programming Foundations course using the Eclipse IDE. His skillset extends into the DevOps sphere, where he is able to deliver courses based around Agile/Scrum practices, version control, and CI/CD.