Contents
Overview
React is a front-end library developed by Facebook.It is a declarative, efficient, and flexible JavaScript library for building user interfaces. It lets you compose complex UIs from small and isolated pieces of code called “components”..
ReactJS allows us to create reusable UI components. It is currently one of the most popular JavaScript libraries and has a strong foundation and large community behind it.
React Key Features
- JSX
- Elements
- Components
- props
- state
- props.children
JSX
JSX is a syntax extension to JavaScript. It is similar to a template language, but it has full power of JavaScript. JSX gets compiled to React.createElement()
calls which return plain JavaScript objects called “React elements”. To get a basic introduction to JSX and find a more in-depth tutorial on JSX here.
React DOM uses camelCase property naming convention instead of HTML attribute names. For example, tabindex
becomes tabIndex
in JSX. The attribute class
is also written as className
since class
is a reserved word in JavaScript:
const name = 'Clementine';
ReactDOM.render(
<h1 className="hello">My name is {name}!</h1>,
document.getElementById('root')
);
Elements
React elements are the building blocks of React applications. One might confuse elements with a more widely known concept of “components”. An element describes what you want to see on the screen. React elements are immutable.
const element = <h1>Hello, world</h1>;
Typically, elements are not used directly, but get returned from components.
Components
React components are small, reusable pieces of code that return a React element to be rendered to the page. The simplest version of React component is a plain JavaScript function that returns a React element:
function Welcome(props) {
return <h1>Hello, {props.name}</h1>;
}
Components can also be ES6 classes:
class Welcome extends React.Component {
render() {
return <h1>Hello, {this.props.name}</h1>;
}
}
Components can be broken down into distinct pieces of functionality and used within other components. Components can return other components, arrays, strings and numbers. A good rule of thumb is that if a part of your UI is used several times (Button, Panel, Avatar), or is complex enough on its own (App, FeedStory, Comment), it is a good candidate to be a reusable component. Component names should also always start with a capital letter (<Wrapper/>
not <wrapper/>
). See this documentation for more information on rendering components.
props
props
are inputs to a React component. They are data passed down from a parent component to a child component.
Remember that props
are readonly. They should not be modified in any way:
// Wrong!
props.number = 42;
If you need to modify some value in response to user input or a network response, use state
instead.
props.children
props.children
is available on every component. It contains the content between the opening and closing tags of a component. For example:
<Welcome>Hello world!</Welcome>
The string Hello world!
is available in props.children
in the Welcome
component:
function Welcome(props) {
return <p>{props.children}</p>;
}
For components defined as classes, use this.props.children
:
class Welcome extends React.Component {
render() {
return <p>{this.props.children}</p>;
}
}
state
A component needs state
when some data associated with it changes over time. For example, a Checkbox
component might need isChecked
in its state, and a NewsFeed
component might want to keep track of fetchedPosts
in its state.
The most important difference between state
and props
is that props
are passed from a parent component, but state
is managed by the component itself. A component cannot change its props
, but it can change its state
. To do so, it must call this.setState()
. Only components defined as classes can have state.
For each particular piece of changing data, there should be just one component that “owns” it in its state. Don’t try to synchronize states of two different components. Instead, lift it up to their closest shared ancestor, and pass it down as props to both of them.