Building Front-End Applications With React - Advanced React Cheatsheet - Codecademy PDF
Building Front-End Applications With React - Advanced React Cheatsheet - Codecademy PDF
Advanced React
React CSS styles
React supports inline CSS styles for components.
Styles are supplied as a style prop to components. // Inline CSS
Inline styles can be written as attributes in the opening <h1 style={{ color: 'red' }}>I am red!
tag of a JSX element, while multi-line styles can be </h1>
stored in a variable and that variable can be invoked
into JSX. // CSS styles stored in a variable and
called in
const color = {
color: 'blue',
background: 'sky'
}
<h1 style={color}>Hello</h1>
Static Property
In React, prop types are set as a static
property( .propTypes ) on a component class or class Birth extends React.Component {
functional component. .propTypes is an object render() { return <h1>{this.props.age}
with property names matching the expected props </h1> }
and values matching the expected value for that prop }
type. The code snippet above demonstrates how
.propTypes can be applied. MyComponent.propTypes = {
age: React.PropTypes.number
}
.isRequired
If a developer wants to indicate that a prop is required
by the component, then the property MyComponent.propTypes = {
.isRequired can be chained to prop types. year:
Doing this will display a warning in the console if the React.PropTypes.number.isRequired
prop is not passed. The code snippet above };
demonstrates the use of .isRequired .
/
Type Checking
In React, propTypes can be used to perform type-
checking on props. This gives developers the ability to
set rules on what data/variable type each component
prop should be and to display warnings when a
component receives invalid type props.