- To prevent syntax repetition you can abstract common properties from the React object using ES6 Destructuring E.g:
//before
import React from 'react';
class Searchbar extends React.Component {
static propTypes = {
property1: React.PropTypes.string.isRequired,
property2: React.PropTypes.any
}
static defaultProps = {
property1: ''
}
}
//after
import React, {Component, PropTypes} from 'react';
class Searchbar extends Component {
static propTypes = {
property1: PropTypes.string.isRequired,
property2: PropTypes.any
}
static defaultProps = {
property1: ''
}
}- Similar to a function and returns
jsxthat gets rendered to the DOM
//Searchbar.js
const Searchbar = () => {
return <input />;
};
//index.js
const App = () => {
return (
<div>
<Searchbar />
</div>
);
}- In functional components the
propsobject is passed as an argument E.g:
const App = (props) => { }-
Used whenever a component needs to have some kind of internal record keeping
-
It is self aware and keeps track of anything that happens to it once it has been rendered
-
It is created using an ES6
class
class Searchbar extends React.Component {
render() {
return <input />;
}
}-
By extending
React.Componentyou give theSearchbarclass added functionality (state and props) -
To call an instance of a
classyou have to wrap it in jsx tags E.g:<Searchbar /> -
Start off with a functional component and as your component gets complex you can convert to a class
-
In a
classcomponentpropsare available within the component and don't need to be passed in as an argument. It can be accessed in a method usingthis.props
-
Similar to a regular event handler, in react it is a function that runs anytime an event occurs
-
In react you declare the event handler then pass it to the element that you want to monitor for events
-
It is considered best practice to begin the name of an event handler with
onorhandleE.g:
class Searchbar extends React.Component {
handleInputChange(e) {
console.log(e.target.value);
}
render() {
return <input onChange={this.handleInputChange()} />;
}
}-
stateis a plain javascript object that is used to record and react to user events -
Each
classbased component has its own state object -
Whenever
stateis changed a component re-renders and also forces all of its children to re-render as well -
Before
statecan be used in a component the object needs to be initialized E.g:
class Searchbar extends React.Component {
constructor(props) {
super(props);
this.state = { term: ''};
}
handleInputChange(e) {
console.log(e.target.value);
}
render() {
return <input onChange={this.handleInputChange()}/>;
}
}-
The
constructorfunction is called whenever a new instance of aclassis created -
It is reserved for setting up certain configurations in a
classsuch as initializing variables, initializing state or binding event handler methods -
superis a reference to theconstructormethod on the React.Componentclassthat is getting extended. This means that is has its ownconstructorfunction and so to reference it we have to use thesuperkeyword -
In the code above
statehas been initialized E.g:this.state = { property1: '' } -
This syntax should only be used to declare state within the
constructorfunction -
this.setState()should be used to manipulate state after it has been initialized E.gthis.setState({ property1: 'new state'});
class Searchbar extends React.Component {
constructor(props) {
super(props);
this.state = { term: ''};
}
render() {
return (
<div>
<input
value={this.state.term}
onChange={(e) => this.setState({term: e.target.value })} />
</div>
);
}
}-
In the code above the value of the input has been set to the
state -
This means that the component is declarative, the
statedetermines how the data or UI is manipulated. -
When an action (or change) occurs the component is already aware through its change in
stateand then re-renders
- The most parent component (App) should be responsible for handling and fetching data in a react app E.g
class App extends Component {
constructor(props) {
super(props);
this.state = { videos: []};
YTSearch({key: API_KEY, term: 'react js'}, (videos) => {
this.setState({ videos })
//this.setSate({ videos: videos});
});
}
render() {
return (
<div>
<Searchbar />
<VideoDetail />
<VideoList />
</div>
);
}
}-
In the code above the
YTSearchmethod is used to return a list of videos to a react app and this information is going to be used by multiple components -
It makes sense to call that method on the
constructorof theAppcomponent which is the parent in this case -
This also makes it possible for every instance of the
Appto have thatYTsearchAPI method available -
To prevent syntax repetition you can make use of ES6 property value shorthand
-
E.g : In the code above when
this.setState()is called within the callback function thevideosarg (which is a variable) is the same as the initializedstate(this.state = { videos: []}) so instead of repeating that variable you can use ES6 to set it as the current state:this.setState({ videos }) -
That is the same as saying :
this.setState({ videos: videos})
//js without ES6
function getCar(make, model, value ) {
return {
make: make ,
model: model,
value: value
};
}
//with ES6
function getcar(make, model, value) {
return {make, model, value};
}-
It is considered best practice to avoid the use of
forloops in react -
You should make use of built-in helpers such as the array
mapmethod E.g
//ES5
var array = [1, 2, 3];
array.map(function(number) { return number * 2});
//ES6
let array = [1, 2, 3];
array.map((number) => { number * 2});- Make use of ES6 template literals to concatenate strings and values E.g:
const VideoDetail = ({video}) => {
// const video = props.video;
const videoId = video.id.videoId;
const url = `https://www.youtube.com/embed/${videoId}`;
return (
<iframe className="embed-responsive-item" src={url}></iframe>
);
}