Fullstack JS Workshop Front End Session Mar 5th, 2017 @ Fak MIPA UGM
About Me
Muhammad Rizki Rijal JS dev @wonderlabs, JS Enthusiast
React JS
Why we use react?
coz it’s cool right? ps: and hype
What is React?
It’s super cool UI library right?
React is a JavaScript library for building user interfaces.
Declarative Component-based Learn once, write anywhere
Declarative Component-based Learn once, write anywhere
I want a fancy button, when it get clicked its color and text will be changing back and forth
Imperative Way
Declarative Way
Declarative Component-based Learn once, write anywhere
Declarative Component-based Learn once, write anywhere
https://engineering.instagram.com/react-native-at-instagram-dd828a9a90c7#.bj6xlq84o
Super Stupidly Simple React App
JSX (HTML in JS?)
Embedding Expression Specifying Attributes Specifying Children
Embedding Expression Specifying Attributes Specifying Children
ProductItem.js ProductItem.css
Embedding Expression Specifying Attributes Specifying Children
Embedding Expression Specifying Attributes Specifying Children
All About Components
Class Component
Functional Component
<CommentList />
<Comment /> <Comment /> <CommentList />
<User /> <User /> <Comment /> <Comment /> <CommentList />
fiddle link
fiddle link
<User /> <User />
fiddle link
<Comment /> <Comment /> <User /> <User />
fiddle link
<CommentList /> <Comment /> <Comment /> <User /> <User />
fiddle link
Props & State
Basically react has two types of data, props and state
Props is read-only data Also it can be used to pass an event/data change handler
State is where to store data that is going to change
later on we can update component’s state with setState()
Data Flow
fiddle link
fiddle link “Store”
fiddle link “View”
fiddle link “Action”
fiddle link Another Example
Component Life Cycle Hooks
Think of it like your daily schedule
Mounting - constructor(props) - componentWillMount() - componentDidMount()
Updating - componentWillReceiveProps(nextProps, prevProps) - shouldComponentUpdate(nextProps, nextState) - componentWillUpdate(nextProps, nextState) - componentDidUpdate()
Unmounting - componentWillUnmount()
What We Gonna Build
Folder Structure /chat-bot /node_modules /public index.html /src /components /pages /lib App.css App.js index.css index.js package.json
<Navbar />
<Navbar /> <ChatWindow />
<Navbar /> <ChatWindow /> <MessageInput />
<Navbar /> <ChatWindow /> <MessageInput /> <MessageList />
<Navbar /> <ChatWindow /> <MessageInput /> <MessageList /> <Message />
<Navbar /> <ChatWindow /> <MessageInput /> <MessageList /> <Message /> <App />
Let’s get started!
Clone UI Template https://github.com/wonderlabshq/js-workshop-html
Dependencies - Create React App $ npm i -g create-react-app $ npm i -S moment axios - Moment (for date creation and formatting) - Axios(Perform request to backend)
App Component State Schema { messages: [ { “author”: “Me”, // “Me” or “Bot” “text”: “Hello!”, // Message body “createdAt”: “Sat Mar 04 2017 20:11:00 GMT+0700 (WIB)”, } ]
Things To Discover Next - React Router - Redux - React Thunk / React Saga / React Observables - Testing - Webpack
Source https://facebook.github.io/react https://tylermcginnis.com/imperative-vs-declarative-programming/
Thanks!

Fullstack JS Workshop

Editor's Notes

  • #2 somethint
  • #10 React is a UI library developed at Facebook to facilitate the creation of interactive, stateful & reusable UI components. It is used at Facebook in production, and Instagram.com is written entirely in React. One of it’s unique selling points is that not only does it perform on the client side, but it can also be rendered server side, and they can work together inter-operably. It also uses a concept called the Virtual DOM that selectively renders subtrees of nodes based upon state changes. It does the least amount of DOM manipulation possible in order to keep your components up to date.
  • #14 The imperative approach is concerned with HOW you’re actually going to get the button. You need to list out the steps to be able to show HOW you’re going to get that.
  • #15 The declarative approach is more concerned with WHAT you want, a togglable button.
  • #16 Build small component, encapsulate logic and compose for a bigger components
  • #17 Build encapsulated components that manage their own state, then compose them to make complex UIs. Since component logic is written in JavaScript instead of templates, you can easily pass rich data through your app and keep state out of the DOM.
  • #19 React Native, Server-side rendering, Electron .etc
  • #23 This funny tag syntax is neither a string nor HTML. It is called JSX, and it is a syntax extension to JavaScript. We recommend using it with React to describe what the UI should look like. JSX may remind you of a template language, but it comes with the full power of JavaScript. JSX produces React "elements"
  • #24 This funny tag syntax is neither a string nor HTML. It is called JSX, and it is a syntax extension to JavaScript. We recommend using it with React to describe what the UI should look like. JSX may remind you of a template language, but it comes with the full power of JavaScript. JSX produces React "elements”
  • #26 You can embed any JavaScript expression in JSX by wrapping it in curly braces. For example, 2 + 2, user.firstName, and formatName(user) are all valid expressions: You can’t use control flow though, but ternary is okay
  • #27 You can embed any JavaScript expression in JSX by wrapping it in curly braces. For example, 2 + 2, user.firstName, and formatName(user) are all valid expressions: You can’t use control flow though, but ternary is okay
  • #28 After compilation, JSX expressions become regular JavaScript objects. This means that you can use JSX inside of if statements and for loops, assign it to variables, accept it as arguments, and return it from functions:
  • #32 JSX tags may contain children
  • #33 Build small components and build the app
  • #34 Build small components and compose it
  • #35 Components let you split the UI into independent, reusable pieces, and think about each piece in isolation. Conceptually, components are like JavaScript functions. They accept arbitrary inputs (called "props") and return React elements describing what should appear on the screen.
  • #36 Components let you split the UI into independent, reusable pieces, and think about each piece in isolation. Conceptually, components are like JavaScript functions. They accept arbitrary inputs (called "props") and return React elements describing what should appear on the screen.
  • #37 Build small components and build the app
  • #38 Build small components and build the app
  • #39 Build small components and build the app
  • #40 Build small components and build the app
  • #41 Build encapsulated components that manage their own state, then compose them to make complex UIs. Since component logic is written in JavaScript instead of templates, you can easily pass rich data through your app and keep state out of the DOM.
  • #42 Build encapsulated components that manage their own state, then compose them to make complex UIs. Since component logic is written in JavaScript instead of templates, you can easily pass rich data through your app and keep state out of the DOM.
  • #43 Build small components and build the app
  • #44 Build encapsulated components that manage their own state, then compose them to make complex UIs. Since component logic is written in JavaScript instead of templates, you can easily pass rich data through your app and keep state out of the DOM.
  • #45 Build small components and build the app
  • #46 Build encapsulated components that manage their own state, then compose them to make complex UIs. Since component logic is written in JavaScript instead of templates, you can easily pass rich data through your app and keep state out of the DOM.
  • #47 Build small components and build the app
  • #48 Build encapsulated components that manage their own state, then compose them to make complex UIs. Since component logic is written in JavaScript instead of templates, you can easily pass rich data through your app and keep state out of the DOM.
  • #49 Components let you split the UI into independent, reusable pieces, and think about each piece in isolation. Conceptually, components are like JavaScript functions. They accept arbitrary inputs (called "props") and return React elements describing what should appear on the screen.
  • #51 In React, application data flows unidirectionally via the state and props objects, as opposed to the two-way binding of libraries like Angular. This means that, in a multi component heirachy, a common parent component should manage the state and pass it down the chain via props. It makes it more predictable and more easy to debug. Your state should be updated using the setState method to ensure that a UI refresh will occur, if necessary. The resulting values should be passed down to child components using attributes that are accessible in said children via this.props. It passed down from “parent” component to “child” component.
  • #53 It’s similar to props, except it is private and fully controlled by the component.
  • #55 It can’t be mutated directly like: this.state = {}, but use setState() instead.
  • #57 Components let you split the UI into independent, reusable pieces, and think about each piece in isolation. Conceptually, components are like JavaScript functions. They accept arbitrary inputs (called "props") and return React elements describing what should appear on the screen.
  • #58 Neither parent nor child components can know if a certain component is stateful or stateless, and they shouldn't care whether it is defined as a function or a class. This is why state is often called local or encapsulated. It is not accessible to any component other than the one that owns and sets it.
  • #59 Neither parent nor child components can know if a certain component is stateful or stateless, and they shouldn't care whether it is defined as a function or a class. This is why state is often called local or encapsulated. It is not accessible to any component other than the one that owns and sets it.
  • #60 Neither parent nor child components can know if a certain component is stateful or stateless, and they shouldn't care whether it is defined as a function or a class. This is why state is often called local or encapsulated. It is not accessible to any component other than the one that owns and sets it.
  • #61 Neither parent nor child components can know if a certain component is stateful or stateless, and they shouldn't care whether it is defined as a function or a class. This is why state is often called local or encapsulated. It is not accessible to any component other than the one that owns and sets it.
  • #62 Neither parent nor child components can know if a certain component is stateful or stateless, and they shouldn't care whether it is defined as a function or a class. This is why state is often called local or encapsulated. It is not accessible to any component other than the one that owns and sets it.
  • #63 Neither parent nor child components can know if a certain component is stateful or stateless, and they shouldn't care whether it is defined as a function or a class. This is why state is often called local or encapsulated. It is not accessible to any component other than the one that owns and sets it. A component may choose to pass its state down as props to its child components This is commonly called a "top-down" or "unidirectional" data flow. Any state is always owned by some specific component, and any data or UI derived from that state can only affect components "below" them in the tree.
  • #64 A component may choose to pass its state down as props to its child components This is commonly called a "top-down" or "unidirectional" data flow. Any state is always owned by some specific component, and any data or UI derived from that state can only affect components "below" them in the tree.
  • #66 Me: Component Teman: Developer Bangun Pagi, Teman mu nitip apa Berangkat ke Kampus, Teman mu nitip apa Pulang Kampus, Teman mu nitip apa
  • #67 The constructor for a React component is called before it is mounted. When implementing the constructor for a React.Component subclass, you should call super(props) before any other statement. Otherwise, this.props will be undefined in the constructor, which can lead to bugs. The constructor is the right place to initialize state. If you don't initialize state and you don't bind methods, you don't need to implement a constructor for your React component. It's okay to initialize state based on props if you know what you're doing. componentWillMount() is invoked immediately before mounting occurs. It is called before render(), therefore setting state in this method will not trigger a re-rendering. componentDidMount() is invoked immediately after a component is mounted. Initialization that requires DOM nodes should go here. If you need to load data from a remote endpoint, this is a good place to instantiate the network request. Setting state in this method will trigger a re-rendering.
  • #68 ComponentWillReceiveProps componentWillReceiveProps() is invoked before a mounted component receives new props. If you need to update the state in response to prop changes (for example, to reset it), you may compare this.props and nextProps and perform state transitions using this.setState() in this method. Note that React may call this method even if the props have not changed, so make sure to compare the current and next values if you only want to handle changes. This may occur when the parent component causes your component to re-render. React doesn't call componentWillReceiveProps with initial props during mounting. It only calls this method if some of component's props may update. Calling this.setState generally doesn't trigger componentWillReceiveProps. 2. shouldComponentUpdate Use shouldComponentUpdate() to let React know if a component's output is not affected by the current change in state or props. The default behavior is to re-render on every state change, and in the vast majority of cases you should rely on the default behavior. shouldComponentUpdate() is invoked before rendering when new props or state are being received. Defaults to true. This method is not called for the initial render or when forceUpdate() is used. Returning false does not prevent child components from re-rendering when their state changes. Currently, if shouldComponentUpdate() returns false, then componentWillUpdate(), render(), and componentDidUpdate() will not be invoked. 3. componentWillUpdate componentWillUpdate() is invoked immediately before rendering when new props or state are being received. Use this as an opportunity to perform preparation before an update occurs. This method is not called for the initial render. Note that you cannot call this.setState() here. If you need to update state in response to a prop change, use componentWillReceiveProps() instead. 4. componentDidUpdate componentDidUpdate() is invoked immediately after updating occurs. This method is not called for the initial render. Use this as an opportunity to operate on the DOM when the component has been updated. This is also a good place to do network requests as long as you compare the current props to previous props (e.g. a network request may not be necessary if the props have not changed).
  • #69 componentWillUnmount() is invoked immediately before a component is unmounted and destroyed. Perform any necessary cleanup in this method, such as invalidating timers, canceling network requests, or cleaning up any DOM elements that were created in componentDidMount