Page Title
the actions and reducers folders as well as the store.js file are related to Redux so we won't worry about that yet.
Let's focus on the index.js and the components folder.
==== index.js ====
The index.js will usually look something like :
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
import './index.css';
ReactDOM.render(
,
document.getElementById('root')
);
**Note this is using es6 so it uses import rather than require.
If we step through what it is doing is it imports the react and react dom dependencies (both necessary) for doing any react magic + syntax. The App import is your component where your app comes together. index.css is just a styling file and is not necessary.
If we look at the render part of the app it is rendering the App component to the root element. It essentially injects the App component into whatever the root element is.
From here on out you can call your components whatever you want but since it is App.js in the example see below for an example App.js file
==== App.js ====
import React, {Component} from 'react';
class App extends Component {
render() {
return (
Welcome to the Example
);
}
}
export default App;
As you can see this App.js is just going to render a h1, nothing amazing, but say we have a component called welcome.js instead
import React, {Component} from 'react';
class Welcome extends Component {
render() {
return (
Welcome to the Example
);
}
}
export default Welcome;
Assuming welcome is in a different package we could then write the App.js like
import React, {Component} from 'react';
import Welcome from './greetings/welcome.js';
class App extends Component {
render() {
return (
);
}
}
export default App;
As you can see we have now created our own tag called Welcome. Just a point to note here, a render method can only return 1 element tag. that element tag can have as many children as you want but only one can be returns for instance you might have your app look like this :
import React, {Component} from 'react';
import Welcome from './greetings/welcome.js';
import Gooddbye from './greetings/goodbye.js';
class App extends Component {
render() {
return (
);
}
}
export default App;
Note the surrounding parent div.
Now lets look at something with a bit more functionality
==== Welcome.js - With some functionality ====
There are a few methods you can override in a React Component, I won't go through them all but the main ones I usually consider are constructor() and componentDidMount(). constructor is called when the component is creating so is used for setting up the state of the component. The state of the component is what is used to rerender the component. The componentDidMount is called right after the component is created so if you were doing and REST or ajax calls I recommend doing them here and setting the state with the results.
import React, {Component} from 'react';
class Welcome extends Component {
constructor(){
super();
this.state = {
count : 1
};
}
componentDidMount(){
this.incresaseCounter();
}
incresaseCounter() {
let counter = this.state.count;
counter++;
this.setState({
count : counter
});
}
render() {
return (
Count : {this.state.count}
);
}
}
export default Welcome;
Right now there is quite a lot going on here. The App doesn't really do anything amazing but this example proves a lot of concepts.
Lets break it down :
constructor(){
super();
this.state = {
count : 1
};
}
The constructor here says called the parent constructor of the Component in the react library, then give this component a state where it has a count of 1.
componentDidMount(){
this.incresaseCounter();
}
Once mounted called the increate counter function. Note that when the page loads it will be so fast loading the counter will say 2 as React is quite fast for loading and making changes, especially something basic like this.
incresaseCounter() {
let counter = this.state.count;
counter++;
this.setState({
count : counter
});
}
Unlike every other function in the Welcome Component, incresaseCounter is not a default component function so it is not overriding anything we are just creating a function.
What is going on here is that a counter variable is being assigned the value of what is currently in the state. The counter is then incremented by 1. The state is then set so the new counter value is in place. Note it is this .setState method that is causing the rerender.
render() {
return (
Count : {this.state.count}
);
}
Finally we have the render method. Note it is returning a single div.
As can be seen the h2 is utilising {} which when used in JSX means find the value of/execute this.
The two things to note about the button tag are the use of className rather than class. Class is a keyword with React/es6 so className should be used to reference styling.
The other thing is the onClick which as you can see points to the incresaseCounter. Since the incresaseCounter funtion will be called as an anonymous function you will need to bind 'this' to it so that when the function is ran via the button, it has proper access to the this you are referring to.
The App should look something like :
{{ :development:applications:screenshot_2017-08-11_12.30.52.png |}}
This has been basic but you can add styling and there are many React packages such as React-Bootstrap for making stuff pretty or you can even just import the Bootstrap css in the html file and write the Bootstrap components as you normal would in HTML inside the JSX.
As a final JavaScript Note remember that ; are use to terminate lines. They are not necessary but if you want to minify your js files then they are necessary.
===== Solution =====
For a sample solution please go to : Dropbox/Errigal Shared Documents/Training/react-basic-starter.zip