Components are an integral part of react. Every part or a react app, from an individual list item, to the app itself is setup as a component. there are a few ways to think about the structure of a component, and your major choices are: Class Component, or Function Component. React Docs on Components and Props
the render function is made to determine what should be rendered to the dom, when the component is used. you should use a return block to set what will be returned from running render. This function is a requirement of a Component.
The return block is JSX, and will let you return ONE parent element (along with nested elements) written in html-like fashion.
- you cannot return more than one element, so siblings will not work: i.e.
// BAD
return (
<h1> hello! <h1>
<h2> This is me </h2>
)
// GOOD
return (
<div className="intro">
<h1> hello! <h1>
<h2> This is me </h2>
</div>
)
// When in doubt, <div> it out!
-
as the above example shows, you cannot use the javascript-reserved keyword
class
when writing your JSX, so you have to useclassName
when referring to an html class. -
all tags, including typical self closing tags like
<br> or <img>
must be explicitly closed, like<br /> or <img />
-
if you need to set a comment within a return block, you must put it inside the parent being returned
\\ BAD
return (
// hello there
<div>
...
\\ CLOSE
return (
{ /* hello there */ }
<div>
...
\\ GOOD
return (
<div>
{ /* hello there */ }
...
Otherwise, you set a comment outside the return block, like a normal // js comment
.
A prop is an attribute, or PROPertieS of a given component. in order to set up props, you:
- set them up on the component, as you would an html attribute:
class Title extends Component
render() {
return (
<div className="title">
<h1>{this.props.firstName}</h1>
<h2>{this.props.hometown}<h2>
...
- Use them when rendering the component itself:
... // inside App.js, inside it's render function, which will be sent to the DOM
<Title firstName="kara" hometown="Denver" />
...
this.props
is an instance object that you have access to for every component being rendered. when you want to access properties of a given component, you can ask via this.props
.... nearly nothing! If you have no need for establishing state within your component, or using the React Lifecycle, it's best to use a SLC, or Stateless Functional Component.
instead of inheriting from React.Component
, you declare the SFC with const, and pipe it through an arrow function.
//you'll still need to import React from 'react', but no need for component!
const Header = () => {
...
}
In the case of a SLC, you don't need to explicitly use render()
. Instead, you can return a JSX object:
const Header = () => {
return (
<div className="container">
<h1>My Cool Website</h1>
</ div>
)
}
Because we're not working within the bounds of a component class, we don't need to use this.props
. instead, if this component needs props passed in, they'll be passed in as props
.
const Header = (props) => {
return (
<div className="container">
<h1>My Cool Website</h1>
<h2> By {props.firstName}</h2>
</ div>
)
}