2. philly.NET labs
• Been doing this for 10 years
• Room gets crowded, arrive early and bring extension cords
• We are not React experts but know enough to get you started
• We are working on live streaming all labs, please be patient!
• Bandwidth is limited at times, keep off the network if possible
• Download key code before you arrive
• This is a community event, please help the person next to you
3. React Lab Series
• November 8
• React intro, tooling, components, JSX syntax, props, state, lifecycle
• December 6
• React routing, styles, server side render, testing
• January 3
• React flux architecture with Redux and RxJS observables
• February 7
• Make the same simple app in Angular, React, and Vue
• December 20
• Monthly meeting compares Angular, React, and Vue
4. React
• Maintained by Facebook and Instagram
• Created by Jordan Walke for internal projects
• Initial open source March 2013
• Currently version 16.0.0 as of Sep 2017
• React Native enables phone apps since March 2015
• React Fiber was introduced in 2017
5. License Controversy
• May 2013 Apache 2.0
• BSD restrictive license followed
• April 2015 updated patent grant
• September 2017 MIT License
6. Virtual DOM
• Virtual Document Object Model
• In-memory data structure
• React does diff with real DOM and efficiently applies changes
7. Components
• Implemented as a JavaScript ES6 class
• Extends React.Component
• Render method is required
• JSX is often used in render but not required
• Accept props
• Can maintain state
• ReactDOM.render method creates a DOM instance of a component
8. JSX
• JavaScript extension syntax
• Nested elements must be wrapped in a single parent
• 16.0.0 allows arrays of elements -> fragments or strings
• Custom attributes require the data- prefix
• Expressions are wrapped in curlies {}
• If/else are not supported but conditional expressions are
9. One way data flow
• Properties are immutable values passed to render method
• Components should not modify props
• “Properties flow down, actions flow up”
• This architecture is referred to as Flux
• Redux is a popular implementation and will be covered in the January lab
10. Tooling
• Visual Studio Code
• Node.js with NPM for package management
• Babel and TypeScript transpilers
• Webpack for bundling
• create-react-app for scaffolding
11. Visual Studio Terminal
• Use terminal mode to run node commands
• Ctrl-`
• Open as many as needed
• Check versions
• node –v
• npm –v
• npm i create-react-app -g
• create-react-app --help
• create-react-app --version
12. create-react-app
• Depends on react-scripts
• Make an ES6 app
• create-react-app react-app
• Review the file structure and package.json
• Make a TypeScript app
• create-react-app react-app-ts --scripts-version=react-scripts-ts
• Review the file structure and package.json
• /public
• /src
• /node_modules
13. Test and Build Your Apps
• cd react-app
• npm start
• Use your browser at localhost:3000
• code .
• Opens a new editor for this project folder
• npm run build
• Makes /build folder
• npm install –g serve
• serve –s build
14. Minimal App
• Remove all files except app.js and index.js
• index.html
• Defines a root element
• index.js
• Renders the App component in the root element
• app.js
• Imports react
• Defines the component class and constructor
• Provides a render method (can use short form stateless function syntax)
15. index.js
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
ReactDOM.render(
<App />,
document.getElementById('root')
);
16. app.js – stateless function
import React from 'react';
const App = () => <h1>Go Eagles!</h1>
export default App;
19. app.js – single wrapper element
class App extends React.Component {
render() {
return (
<div>
<h1>Go Eagles!</h1>
<h3>beat Dallas</h3>
</div>
);
}
}
20. Properties
• How static data is passed between components
• propTypes
• Object with keys that define parameter signature
• defaultProps
• Object with keys for default values
24. State
• Properties that will change value
• Call super() in constructor to create context
• Define state object with default values
• Typical to use update() method to change state
• Later we will use lifecycle events to fetch external data to state