React Basic
React Basics
This is a draft version of a react outline from basics. Send PRs to correct any misconceptions.
Resources
- react-boilerplate
- React official docs
- plot.ly React Tutorial
- Part 1: This section is confusingly written but the example is quite nice. See below.
- Part 2-6: The weather app seems like a nice example, but I don't plan to use create-react-app
- Plan: Try building this part in a static file with a transpiler service?
- Result: Parts 2, 3 are complete, using babel-standalone transpiler served from cdnjs
- Babel is the de facto JSX transpiler.
- babel-standalone - might be used as a static site transpiler?
- Official React Tutorial
Getting Started
-
Basics Capabilities - React Official Docs
- React can be used in:
- Frontend Stack
- React Native (mobile)
- Server Side (node)
- React Components:
- Keep state out of DOM
- Encapsulated
- Compose them to make complex UIs
- Build them
- Declarative Views
- Design Simple Views for each state in your application
- React efficiently updates and renders just the right components when the data changes
- React can be used in:
Plot.ly Introduction - Weather App
-
Who uses react? Netflix, Airbnb, etc.
-
Why React? For "building large applications with data that changes over time"
- Components: composable, reusable, encapsulated
-
Lets Build: A Counter that updates state.
- react.jsbin.com - a fully featured react environment
-
React is 2 Libraries:
React
- allows you to createReactElements
ReactDOM
- renders theReactElements
- Why the split? You could theoretically render those
ReactElements
anywhere -
Boilerplate HTML for this section
html <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>React</title> <link rel="stylesheet" href="http://cdnjs.cloudflare.com/ajax/libs/normalize/3.0.1/normalize.css"> <script src="//fb.me/react-with-addons-0.14.3.js"></script> <script src="//fb.me/react-dom-0.14.3.js"></script> </head> <body> <div id="container"></div> </body> </html>
-
Example
javascript ReactDOM.render( React.createElement('h1', {className: 'heading'}, 'Hello World!'), document.getElementById('container') );
-
ReactDOM.render()
will render aReactElement
created byReact.createElement()
- The DOM node we want to render (2nd argument) is called the
entry point
ReactElements
take three arguments:- The node we want to create (HTML Element)
- A JavaScript Object of information (like 'type')
- Optionally a child such as
innerText
or anotherReactElement
- A child element like this does get
null
for the second argument
- A child element like this does get
- The DOM node we want to render (2nd argument) is called the
-
Components: Creating a
ReactComponent
-
Component is a function that receives
props
- short for properties```javascript // What is the purpose of props here? Does it allow us to pass the child ReactElement? var Wrapper = function(props) { return( React.createElement('div', { className: 'wrapper'}, props.children); ); } React.createElement(Wrapper, {}, 'Hello World!');
ReactDOM.render( React.createElement(Wrapper, null, React.createElement('h1', { className: 'heading' }, 'Hello World!') ), document.getElementById('container') ); ```
-
-
Converting our
ReactComponent
toJSX
-
JSX
is 'nothing but a wrapper onReact.createElement
- To use Javascript in JSX, wrap it in curly braces.
JSX
always must be transpiled with a build tool.
-
How to
transpile
JSX in your build environment?- Babel / Gulp -
npm install --save-dev babel-cli babel-preset-react
javascript var Wrapper = function(props) { return ( <div className="wrapper">{ props.children }</div> ); }; ReactDOM.render( <Wrapper> <h1 className="heading">Hello World!</h1> </Wrapper>, document.getElementById('container') );
7. Classes - 1. Event handlers likeonClick
,onMouseOver
, etc. CANNOT go in a ReactComponent. - These handlers must be attached to an actual DOM node. - This 'Counter' object actually demonstrates passing Event Handlers.```javascript class Counter extends React.Component { render() { return ( <div> <p>This is a Counter component!</p> <Button text="Click me!" onClick={function() { console.log('click!') }} /> </div> ); } } // Button pulls `props` from ReactComponent JSX (React.CreateElement) var Button = function(props) { return ( <button onClick={ props.onClick }>{ props.text }</button> ); } ReactDOM.render( <Counter /> document.getElementById('container') ); ```
-
Handling State - State is a plain object in react, with any number of properties.
constructor
is used to assign initial state- Lets make counter do what it is supposed to do with state.
-
The
{ }
JSX notation works with any variable, example{ myPotato }
``javascript class Counter extends React.Component { constructor() { super(); this.state = { clicks: 0 }; // "
thisis undefined in
incrementbecause of the way
ES6 Classeswork // This explanation is terrible. Why again must we bind
this? // Trent: According to MDN, bind will make a new
bound functionand bind whatever you pass as
this` for that function. The bound function wraps the original function object. // Trent: But why is increment set in the constructor? Is increment constructed before the constructor? Maybe... not sure. Alternative idea: Can you bind a function and then define it? this.increment = this.increment.bind(this); }increment() { this.setState({ clicks: this.state.clicks + 1 }); }; render() { return ( <div> <p>This is a Counter component! The button was clicked { this.state.clicks } times.</p> <Button text="Click me!" onClick={function() { console.log('click!') }} /> </div> ); }
} // Button pulls
props
from ReactComponent JSX (React.CreateElement) var Button = function(props) { return ( ); } ReactDOM.render(document.getElementById('container') ); ``` -
Whenever the state changes, the
<p>
element is updated.
-
Plot.ly Part II - Your First App
- New Concepts
- XMLHttpRequest asynchronous w/ event listener triggers React state change
- Receiving data with an API on the client side
- Note that you can set a cookie for the client and use it to authenticate against your own API for that user...
- handling
this
by alias inside a child function with its ownthis
- Using arrow functions to manage state.
- Using
preventDefault
to prevent a form's default submit behavior - Declaring a variable
currentTemp
and managing its default value and state.
- XMLHttpRequest asynchronous w/ event listener triggers React state change
Plot.ly Part III - Plot.ly - Adding a Forecast Graph
- New Concepts
- ComponentDidMount, ComponentDidUpdate
- build
Plot
component around plot.ly library (imported with cdnjs) - Interacting with a plot.ly graph's
plotly_click
event handler. - Managing state with a complex UI -- still kind of a rats nest.
- Tie plot.ly event handler and CSS IDs into onPlotClick component (props.onPlotClick)