React: Installation
React: Installation
React: Installation
Installation
Here we will install the create-react-app tool using the
Node Package Manager (NPM). This tool is developed
by the React.js team. First is to install the Node.js.
Meiert, 2017
Basic Principles of Website Design
• Grid system
• Web-safe fonts
• Visual hierarchy
• Use of colors and images
Principles of Ethical Web Development
• Web applications should:
• work for everyone
• work everywhere
• Respect a user’s privacy and security
• Web developers should be considerate of their
peers
ITELEC4C
Introduction to ReactJS
ReactJS
• Simple, feature rich, component-based JavaScript
UI library.
• Allows developers to create reusable UI
components
• A library to create UI in a web application
Jordan Walke
Timeline
2010 • The Inception
Learning Objectives
At the end of this module, the learner will be able to:
▪ Develop understanding of the basic concepts of ReactJS
including JSX and elements.
COLLEGE OF INFORMATION AND COMPUTING SCIENCES
Introducing JSX
JSX
- Syntax extension of JS.
- Describes what the UI should look like
- JSX produces React “elements”
COLLEGE OF INFORMATION AND COMPUTING SCIENCES
Why JSX?
Why JSX?
You can put any valid JavaScript expression inside the curly
braces in JSX. For example, 2 + 2, user.firstName, or
formatName(user) are all valid JavaScript expressions.
COLLEGE OF INFORMATION AND COMPUTING SCIENCES
JSX is an Expression
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:
COLLEGE OF INFORMATION AND COMPUTING SCIENCES
Attributes in JSX
Warning: Don’t put quotes around curly braces when embedding a JavaScript expression in
an attribute. You should either use quotes (for string values) OR curly braces (for
expressions), but not both in the same attribute.
COLLEGE OF INFORMATION AND COMPUTING SCIENCES
React reads these objects and uses them to construct the DOM
and keep it up to date.
COLLEGE OF INFORMATION AND COMPUTING SCIENCES
What is “Element”?
Note:
One might confuse elements with a more widely known concept of
“components”. Elements are what components are “made of”.
COLLEGE OF INFORMATION AND COMPUTING SCIENCES
Open in CodePen
COLLEGE OF INFORMATION AND COMPUTING SCIENCES
Open in CodePen
COLLEGE OF INFORMATION AND COMPUTING SCIENCES
– End of Module –
COLLEGE OF INFORMATION AND COMPUTING SCIENCES
COLLEGE OF INFORMATION AND COMPUTING SCIENCES
Reference
Learning Objectives
At the end of this module, the learner will be able to:
▪ Develop understanding on React components and props.
COLLEGE OF INFORMATION AND COMPUTING SCIENCES
Components
Components
Rather than building the whole UI under one single file, we can
and we should divide all the sections (marked with red) into
smaller independent pieces. In other words, these are
components.
Functional Components
Class Components
Class components are ES6 classes that return JSX. Below, you
see our same Welcome function, this time as a class
component:
COLLEGE OF INFORMATION AND COMPUTING SCIENCES
Rendering a Component
Try in CodePen
COLLEGE OF INFORMATION AND COMPUTING SCIENCES
Components
So a Functional Component in React:
■ is a JavaScript/ES6 function
■ must return a React element (JSX)
■ always starts with a capital letter (naming convention)
■ takes props as a parameter if necessary
COLLEGE OF INFORMATION AND COMPUTING SCIENCES
Rendering a Component
COLLEGE OF INFORMATION AND COMPUTING SCIENCES
Composing Components
Components can refer to other components in their output. This lets
us use the same component abstraction for any level of detail.
COLLEGE OF INFORMATION AND COMPUTING SCIENCES
Extracting Components
Comment Component
Try in CodePen
COLLEGE OF INFORMATION AND COMPUTING SCIENCES
Avatar Component
Try in CodePen
COLLEGE OF INFORMATION AND COMPUTING SCIENCES
Got Tired?
Reference
▪ Install react-beautiful-dnd
npm i react-beautiful-dnd --save
COLLEGE OF INFORMATION AND COMPUTING SCIENCES
react-beautiful-dnd
App.css
COLLEGE OF INFORMATION AND COMPUTING SCIENCES
App.js
COLLEGE OF INFORMATION AND COMPUTING SCIENCES
COLLEGE OF INFORMATION AND COMPUTING SCIENCES
COLLEGE OF INFORMATION AND COMPUTING SCIENCES
COLLEGE OF INFORMATION AND COMPUTING SCIENCES
COLLEGE OF INFORMATION AND COMPUTING SCIENCES
COLLEGE OF INFORMATION AND COMPUTING SCIENCES
COLLEGE OF INFORMATION AND COMPUTING SCIENCES
COLLEGE OF INFORMATION AND COMPUTING SCIENCES
Components cont.
State and Lifecycle
COLLEGE OF INFORMATION AND COMPUTING SCIENCES
Learning Objectives
At the end of this module, the learner will be able to:
▪ convert functions to classes.
▪ add local state to a class
▪ add lifecycle method to a class
COLLEGE OF INFORMATION AND COMPUTING SCIENCES
Try in CodePen
COLLEGE OF INFORMATION AND COMPUTING SCIENCES
1. Replace
this.date with
this
state.data in
the render()
method
COLLEGE OF INFORMATION AND COMPUTING SCIENCES
2. Add a constructor
that assigns the
initial this.state
COLLEGE OF INFORMATION AND COMPUTING SCIENCES
Try in CodePen
COLLEGE OF INFORMATION AND COMPUTING SCIENCES
Lifecycle methods
componentDidMount()
method runs after the component output has been
rendered to the DOM.
COLLEGE OF INFORMATION AND COMPUTING SCIENCES
Lifecycle methods
componentWillUnmount()
is invoked immediately before a component is
unmounted and destroyed.
COLLEGE OF INFORMATION AND COMPUTING SCIENCES
Try in CodePen
COLLEGE OF INFORMATION AND COMPUTING SCIENCES
Using setState( )
Using setState( )
Using setState( )
▪ State updates may be asynchronous
COLLEGE OF INFORMATION AND COMPUTING SCIENCES
Using setState( )
Reference
Learning Objectives
At the end of this module, the learner will be able to:
▪ Understand the form and user input.
▪ Create an application using forms and user input
COLLEGE OF INFORMATION AND COMPUTING SCIENCES
Forms
▪ React uses forms to allow users to interact with the web
page.
▪ Controlled
− React provides a special attribute, value for all input
elements, and controls the input elements.
▪ Uncontrolled
− React provides minimal support for form programming.
COLLEGE OF INFORMATION AND COMPUTING SCIENCES
Controlled Components
textarea
▪ uses
a value attribute
COLLEGE OF INFORMATION AND COMPUTING SCIENCES
select
• React uses
a value attribute
on the
root select tag.
COLLEGE OF INFORMATION AND COMPUTING SCIENCES
References
▪ https://reactjs.org/docs/forms.html
▪ https://www.w3schools.com/react/react_forms.asp
▪ https://www.tutorialspoint.com/reactjs/reactjs_forms.htm
COLLEGE OF INFORMATION AND COMPUTING SCIENCES
Events
COLLEGE OF INFORMATION AND COMPUTING SCIENCES
Learning Objectives
At the end of this module, the learner will be able to:
▪ Discuss the importance of events.
▪ Create an application using forms and user input with
events
COLLEGE OF INFORMATION AND COMPUTING SCIENCES
Events
3. In react, we cannot
return false to prevent
the default behavior.
COLLEGE OF INFORMATION AND COMPUTING SCIENCES
Example
COLLEGE OF INFORMATION AND COMPUTING SCIENCES
REACT JS:
DYNAMIC FORMS
COLLEGE OF INFORMATION AND COMPUTING SCIENCES
Create a Form
COLLEGE OF INFORMATION AND COMPUTING SCIENCES
onChange event
• Create a function called handleFormChange.
COLLEGE OF INFORMATION AND COMPUTING SCIENCES
onChange event
• Assign this function to the input fields as an onChange
event.
COLLEGE OF INFORMATION AND COMPUTING SCIENCES
onChange event
• This onChange event takes two parameters, index and
event. Index is the index of the array and event is the data
we type in the input field. We are passing those to the
handleFormChange function.
COLLEGE OF INFORMATION AND COMPUTING SCIENCES
onChange event
• Let's store our inputFields state into a variable called data
using the spread operator (the three dots ...).
• Then, we will target the index of the data variable using
the index parameter, and the name of the property, too.
COLLEGE OF INFORMATION AND COMPUTING SCIENCES
onChange event
• Now, we need to store this data back inside the
inputFields array using the setInputFields method.
COLLEGE OF INFORMATION AND COMPUTING SCIENCES
REACT JS:
LIFTING STATE UP
COLLEGE OF INFORMATION AND COMPUTING SCIENCES
Lifting State Up
• Often, several components need to reflect the same
changing data. We recommend lifting the shared state
up to their closest common ancestor. Lifting state up is a
common pattern that is essential for React developers to
know. It helps you avoid more complex (and often
unnecessary) patterns for managing your state.
COLLEGE OF INFORMATION AND COMPUTING SCIENCES
Lifting State Up
• We will start with a component called BoilingVerdict. It
accepts the celsius temperature as a prop, and prints
whether it is enough to boil the water:
COLLEGE OF INFORMATION AND COMPUTING SCIENCES
Lifting State Up
• Next, we will create a component called Calculator. It
renders an <input> that lets you enter the temperature,
and keeps its value in this.state.temperature.
• Additionally, it renders the BoilingVerdict for the current
input value.
COLLEGE OF INFORMATION AND COMPUTING SCIENCES
Lifting State Up
COLLEGE OF INFORMATION AND COMPUTING SCIENCES
Adding
Second
Input
COLLEGE OF INFORMATION AND COMPUTING SCIENCES
If the Calculator owns the shared state, it becomes the “source of truth”
for the current temperature in both inputs. It can instruct them both to
have values that are consistent with each other. Since the props of
both TemperatureInput components are coming from the same
parent Calculator component, the two inputs will always be in sync.
COLLEGE OF INFORMATION AND COMPUTING SCIENCES
Remember…
Every component in React has its own state, and
because of this sometimes data can be redundant and
inconsistent. So, by Lifting up the state we make the
state of the parent component as a single source of
truth and pass the data of the parent in its children.
COLLEGE OF INFORMATION AND COMPUTING SCIENCES
References
• https://www.geeksforgeeks.org/lifting-state-up-in-reactjs/
• https://reactjs.org/docs/lifting-state-up.html
may * sa dulo ung di ko sure
1. When we are developing web applications, it must be fast, integrated, reliable, and engaging.
● True
2. ReactJS focuses building reusable user interfaces
● True
3. In React, same components can be used on other libraries/frameworks.
● True*
4. A React components MUST always start with an UPPERCASE letter such as NewComp.js,
Header.js
● True
5. Every time state of an object changes, React re-renders the component.
● True
18. What is the default local host port that a React development server uses?
● 3000
19. Components can be implemented in ____.
● Either functions or classes.*
20. Smallest building blocks of React apps.
● Elements
21. Components are composed of _____.
● Elements
22. ReactJS was initiated by:
● Facebook
23. A JS extension allows you to write HTML codes inside JS.
● JSX
24. A reusable code block which divides the UI into smaller pieces.
● Components
25. React allows developers to do the follow EXCEPT:
● Make UI codes more complicated
26. In React what is used to pass data to a component from outside?
● Props
27. What would be the output of the code below?
False
False
If you will develop web application using ReactJS, you can use ___ to allow the target users to
interact with the web pages.
Forms
It is possible to create a web application that can change its content during run time.
True
Events
Synthetic events are the objects which act as cross-browser wrapper around the browser’s
native event.
True
It is required to bind this inside the constructor when using an arrow function.
False
Developer can add form with React like any other elements.
True
It seems that the code has a problem, the submit button is not working properly. The pop-up
message showing the program I selected does not appear where i click the button what could
be the problem.
https://codepen.io/levisaz-the-decoder/pen/rNJMXNK?editors=1010
False
In React, sharing state is done by moving it to the closest common ancestor of the components
that need it.
True
This returns the value of the element that triggered the event.
event.target.value
What seems to be the problem with this code? Every time I type in the text box, an alert box
appears. Identify what line number has a problem and its justification.
Line 23 on change….
setState()
It seems that the code has a problem, the Submit button is not working properly. The pop-up
message did not display when i click the button, what could be the problem
=>
It is required for each component to have a render ().
True
This is used for comparison two variables together with its data type.
===
If there are more than one element that are need to render, those elements must be inside of a
parent tag like:
<div>
What seems to be the problem with this code? Every time I type in the text box, an alert box
appears. Identify what line number has a problem and its justification.
The event argument contains a set of properties, which are specific to an event.
True
This is used for comparison between two variables irrespective of the data type of variable.
==
The method can be use if you want to cancel an event for instance on clicking a Submit button
and you want to avoid it from submitting.
preventDefault()
Synthetic Event
What is missing in the code below?