SlideShare uma empresa Scribd logo
1 de 18
ReactJS an Intro
Vijayakanth M
DevOps Engineer
Megam Systems
1
AGENDA
2
1. What is ReactJS and Why ?
2. ReactJS with JSX
3. Lifecycle Methods (specs and state)
4. Some code
5. Installation and Config
3
❖ ReactJS is a JavaScript Library for creating user interfaces by Facebook
and Instagram.
❖ ReactJS is a building block of Components.
❖ In ReactJS, application data flows unidirectionally via the state and props
objects, contrast to frameworks like Angular and Ember.
❖ ReactJS runs a “diffing” algorithm, which identifies that has changed.
❖ This will serve purely as an API endpoint which we'll use for getting and
saving data.
What is ReactJS ?
4
Flow of ReactJS
MVC Process ReactJS Flux Process
Why do we use ReactJS ?
❖ ReactJS is extremely fast, because it uses virtual DOM, and syncs only the
changed parts.
❖ React.js uses a special syntax called JSX, which allows you to mix HTML
with JavaScript.
❖ When the data changes, ReactJS conceptually hits the "refresh" button,
and knows to only update the changed parts.
❖ React is all about building reusable components.
❖ It is much smaller and plays nicely with jQuery and other frameworks.
6
ReactJS with JSX
❖ JSX is a JavaScript syntax extension that looks similar to
XML. You can use a simple JSX syntactic transform with React.
❖ React JSX transforms from an XML-like syntax into native
JavaScript.
var Nav, Profile;
// Input (JSX):
var app = <Nav color="blue"><Profile>click</Profile></Nav>;
// Output (JS):
var app = React.createElement(
Nav, {color:"blue"},
React.createElement(Profile, null, "click")
);
7
Now see a simple structure of ReactJS Component
var MyComponent = React.createClass({
render: function(){
return (
<h1>Hello, world!</h1>
<CommentList />
);}
});
How to call the Components in Rails
<% react_component ‘MyComponent’ %>
var CommentList = React.createClass({
render: function() {
return (
<div className="commentList">
<h1> I am a CommentList. </h1>
</div>
);
}
});
Every Component must have a render function,
it will return an another react Component or
tags
8
Lifecycle Methods, Specs and State
8
componentWillMount Invoked once, on both client &
server before rendering occurs.
componentDidMount Invoked once, only on the client,
after rendering occurs.
shouldComponentUpdate Return value determines whether
component should update
componentWillUnmount Invoked prior to unmounting
component.
9
continue….
State
Every component has a state object and a props object. State is set using
the setState method. Calling setState triggers UI updates and is the bread
and butter of React’s interactivity. If we want to set an initial state before
any interaction occurs we can use the getInitialState method.
getInitialState Return value is the initial value for state.
getDefaultProps Sets fallback props values if props aren’t
supplied.
Specs
10
some code
//Child component
var Comment =
React.createClass({
render: function() {
return (
<div className="comment">
<h2
className="commentAuthor">
{this.props.author}
</h2>
{this.props.children}
</div>
);
} });
//Parent component
var CommentList = React.createClass({
render: function() {
return (
<div className="commentList">
<Comment author="Pete Hunt">This is one
comment</Comment>
<Comment author="Jordan Walke">This is
*another* comment</Comment>
</div>
);
}
});
<%= react_component ‘CommentList’ %>
Note that we have passed some data from the parent CommentList component to the child Comment
components. For example, we passed PeteHunt (via an attribute) and This is one comment (via an XML-
like child node) to the first Comment. As noted above, the Comment component will access these
'properties' through this.props.author, and this.props.children.
11
var data = [
{author: "Pete Hunt", text: "This is one comment"},{},...}
];
var CommentBox = React.createClass({
loadCommentsFromServer: function() {
$.ajax({
//… Make a ajax call to get data from server
}); },
getInitialState: function() {
return {data: []};
},
// to get data continuous
componentDidMount: function() {
this.loadCommentsFromServer();
setInterval(this.loadCommentsFromServer,
this.props.pollInterval);
},
// Render another Components
render: function() {
return (
<div className="commentBox">
<h1>Comments</h1>
<CommentList data={this.state.data} />
</div>
);
}
});
Data from the server
12
continue...
// To list all data get from the server
var CommentList = React.createClass({
render: function() {
var commentNodes = this.props.data.map(function
(comment) {
return (
<Comment author={comment.author}>
{comment.text}
</Comment>
);
});
return (
<div className="commentList">
{commentNodes}
</div>
);
}
var Comment = React.createClass({
render: function() {
return (
<div className="comment">
<h2 className="commentAuthor">
{this.props.author}
</h2>
{this.props.children}
</div>
);
}
});
13
continue...
<%= react_component ‘CommentBox’ url: “/json”, timeInterval: 4000 %>
Or
<script type="text/javascript">
ReactDOM.render( ReactDOM.render(React.createElement(NewOverview, { url: “/json”,
timeInterval:4000}), document.getElementById('content') );
</script>
Write the Component code in :
app/assets/javascripts/components/<filename>.js.jsx
the calling command on:
app/views/<directory name>/<htmlerbfile>.html.erb
The flow of the Program
14
Flow of above Components
CommentBox //Parent component
getInitialState { }
loadCommentsFromServer{ $.ajax({ //… Make a ajax call to get data }); },
componentDidMount {setInterval( Get synchronous ajax call to get data) }
CommentList //Child component
Map{ // fetch each data from whole json and send to component }
Comment //Child component
render: { return ( Call children components
and send data)}
render: { return ( call Comment component)}
render: { return ( Show all data )}
15
continue...
Events
React attaches event handlers(onSubmit) to components using a camelCase naming
convention.
Call preventDefault() on the event to prevent the browser's default action of submitting
the form.
Refs We use the ref attribute to assign a name to a child component and this.refs to
reference the component. We can call React.findDOMNode(component) on a
component to get the native browser DOM element.
Callbacks as props
We need to pass data from the child component back up to its parent. We do this in our
parent's render method by passing a new callback ( handleCommentSubmit) into the
child, binding it to the child's onCommentSubmit event. Whenever the event is triggered,
the callback will be invoked:
16
Installation and Config
Add react-rails to your gemfile:
gem 'react-rails', '~> 1.3.0'
Next, run the installation script:
rails g react:install
place the following in your
application.js(app/assets/javascripts/):
//= require react
//= require react_ujs
//= require components
You can pick which React.js build
(development, production, with or
without add-ons) to serve in each
environment by adding a config.
# config/environments/development.rb
MyApp::Application.configure do
config.react.variant = :development
end
# config/environments/production.rb
MyApp::Application.configure do
config.react.variant = :production
end
Questions...
18
Vijayakanth M
DevOps Engineer
Megam Systems
(www.megam.io)
Twitter: @vijayakanth_mp
Email: mvijaykanth@megam.io
Docs: docs.megam.io
Devcenter: Devcenter.megam.io
Thank you…

Mais conteúdo relacionado

Mais procurados

Getting Started with React-Nathan Smith
Getting Started with React-Nathan SmithGetting Started with React-Nathan Smith
Getting Started with React-Nathan SmithTandemSeven
 
ReactJS presentation
ReactJS presentationReactJS presentation
ReactJS presentationThanh Tuong
 
Introduction to React JS
Introduction to React JSIntroduction to React JS
Introduction to React JSArno Lordkronos
 
React JS - A quick introduction tutorial
React JS - A quick introduction tutorialReact JS - A quick introduction tutorial
React JS - A quick introduction tutorialMohammed Fazuluddin
 
[Final] ReactJS presentation
[Final] ReactJS presentation[Final] ReactJS presentation
[Final] ReactJS presentation洪 鹏发
 
React and Flux life cycle with JSX, React Router and Jest Unit Testing
React and  Flux life cycle with JSX, React Router and Jest Unit TestingReact and  Flux life cycle with JSX, React Router and Jest Unit Testing
React and Flux life cycle with JSX, React Router and Jest Unit TestingEswara Kumar Palakollu
 
Introduction to ReactJS
Introduction to ReactJSIntroduction to ReactJS
Introduction to ReactJSKnoldus Inc.
 
React JS: A Secret Preview
React JS: A Secret PreviewReact JS: A Secret Preview
React JS: A Secret Previewvaluebound
 
React js t1 - introduction
React js   t1 - introductionReact js   t1 - introduction
React js t1 - introductionJainul Musani
 
Introduction to React JS for beginners | Namespace IT
Introduction to React JS for beginners | Namespace ITIntroduction to React JS for beginners | Namespace IT
Introduction to React JS for beginners | Namespace ITnamespaceit
 
Comparing Angular and React JS for SPAs
Comparing Angular and React JS for SPAsComparing Angular and React JS for SPAs
Comparing Angular and React JS for SPAsJennifer Estrada
 

Mais procurados (20)

Getting Started with React-Nathan Smith
Getting Started with React-Nathan SmithGetting Started with React-Nathan Smith
Getting Started with React-Nathan Smith
 
React render props
React render propsReact render props
React render props
 
reactJS
reactJSreactJS
reactJS
 
ReactJS presentation
ReactJS presentationReactJS presentation
ReactJS presentation
 
Introduction to React JS
Introduction to React JSIntroduction to React JS
Introduction to React JS
 
ReactJS for Beginners
ReactJS for BeginnersReactJS for Beginners
ReactJS for Beginners
 
React JS - A quick introduction tutorial
React JS - A quick introduction tutorialReact JS - A quick introduction tutorial
React JS - A quick introduction tutorial
 
[Final] ReactJS presentation
[Final] ReactJS presentation[Final] ReactJS presentation
[Final] ReactJS presentation
 
React and Flux life cycle with JSX, React Router and Jest Unit Testing
React and  Flux life cycle with JSX, React Router and Jest Unit TestingReact and  Flux life cycle with JSX, React Router and Jest Unit Testing
React and Flux life cycle with JSX, React Router and Jest Unit Testing
 
Reactjs
Reactjs Reactjs
Reactjs
 
Introduction to ReactJS
Introduction to ReactJSIntroduction to ReactJS
Introduction to ReactJS
 
React workshop
React workshopReact workshop
React workshop
 
React JS: A Secret Preview
React JS: A Secret PreviewReact JS: A Secret Preview
React JS: A Secret Preview
 
React js
React jsReact js
React js
 
React JS .NET
React JS .NETReact JS .NET
React JS .NET
 
React js t1 - introduction
React js   t1 - introductionReact js   t1 - introduction
React js t1 - introduction
 
Introduction to React JS for beginners | Namespace IT
Introduction to React JS for beginners | Namespace ITIntroduction to React JS for beginners | Namespace IT
Introduction to React JS for beginners | Namespace IT
 
React js for beginners
React js for beginnersReact js for beginners
React js for beginners
 
React and redux
React and reduxReact and redux
React and redux
 
Comparing Angular and React JS for SPAs
Comparing Angular and React JS for SPAsComparing Angular and React JS for SPAs
Comparing Angular and React JS for SPAs
 

Semelhante a Intro react js

React Hooks Best Practices in 2022.pptx
React Hooks Best Practices in 2022.pptxReact Hooks Best Practices in 2022.pptx
React Hooks Best Practices in 2022.pptxBOSC Tech Labs
 
Build web apps with react js
Build web apps with react jsBuild web apps with react js
Build web apps with react jsdhanushkacnd
 
React - Start learning today
React - Start learning today React - Start learning today
React - Start learning today Nitin Tyagi
 
React, Flux and more (p1)
React, Flux and more (p1)React, Flux and more (p1)
React, Flux and more (p1)tuanpa206
 
React & Redux for noobs
React & Redux for noobsReact & Redux for noobs
React & Redux for noobs[T]echdencias
 
Content-Driven Apps with React
Content-Driven Apps with ReactContent-Driven Apps with React
Content-Driven Apps with ReactNetcetera
 
Introduction to React JS for beginners
Introduction to React JS for beginners Introduction to React JS for beginners
Introduction to React JS for beginners Varun Raj
 
class based component.pptx
class based component.pptxclass based component.pptx
class based component.pptxsaikatsamanta49
 
MeetJS Summit 2016: React.js enlightenment
MeetJS Summit 2016: React.js enlightenmentMeetJS Summit 2016: React.js enlightenment
MeetJS Summit 2016: React.js enlightenmentArtur Szott
 
OttawaJS - React
OttawaJS - ReactOttawaJS - React
OttawaJS - Reactrbl002
 
Top 10 Techniques For React Performance Optimization in 2022.pptx
Top 10 Techniques For React Performance Optimization in 2022.pptxTop 10 Techniques For React Performance Optimization in 2022.pptx
Top 10 Techniques For React Performance Optimization in 2022.pptxBOSC Tech Labs
 
How To Utilize Context API With Class And Functional Componen in React.pptx
How To Utilize Context API With Class And Functional Componen in React.pptxHow To Utilize Context API With Class And Functional Componen in React.pptx
How To Utilize Context API With Class And Functional Componen in React.pptxBOSC Tech Labs
 
A full introductory guide to React
A full introductory guide to ReactA full introductory guide to React
A full introductory guide to ReactJean Carlo Emer
 
React + Redux + TypeScript === ♥
React + Redux + TypeScript === ♥React + Redux + TypeScript === ♥
React + Redux + TypeScript === ♥Remo Jansen
 
Adding a modern twist to legacy web applications
Adding a modern twist to legacy web applicationsAdding a modern twist to legacy web applications
Adding a modern twist to legacy web applicationsJeff Durta
 

Semelhante a Intro react js (20)

React Hooks Best Practices in 2022.pptx
React Hooks Best Practices in 2022.pptxReact Hooks Best Practices in 2022.pptx
React Hooks Best Practices in 2022.pptx
 
Build web apps with react js
Build web apps with react jsBuild web apps with react js
Build web apps with react js
 
ReactJS
ReactJSReactJS
ReactJS
 
React - Start learning today
React - Start learning today React - Start learning today
React - Start learning today
 
ReactJs
ReactJsReactJs
ReactJs
 
React, Flux and more (p1)
React, Flux and more (p1)React, Flux and more (p1)
React, Flux and more (p1)
 
React & Redux for noobs
React & Redux for noobsReact & Redux for noobs
React & Redux for noobs
 
Content-Driven Apps with React
Content-Driven Apps with ReactContent-Driven Apps with React
Content-Driven Apps with React
 
react-slides.pptx
react-slides.pptxreact-slides.pptx
react-slides.pptx
 
Introduction to React JS for beginners
Introduction to React JS for beginners Introduction to React JS for beginners
Introduction to React JS for beginners
 
class based component.pptx
class based component.pptxclass based component.pptx
class based component.pptx
 
MeetJS Summit 2016: React.js enlightenment
MeetJS Summit 2016: React.js enlightenmentMeetJS Summit 2016: React.js enlightenment
MeetJS Summit 2016: React.js enlightenment
 
OttawaJS - React
OttawaJS - ReactOttawaJS - React
OttawaJS - React
 
Top 10 Techniques For React Performance Optimization in 2022.pptx
Top 10 Techniques For React Performance Optimization in 2022.pptxTop 10 Techniques For React Performance Optimization in 2022.pptx
Top 10 Techniques For React Performance Optimization in 2022.pptx
 
How To Utilize Context API With Class And Functional Componen in React.pptx
How To Utilize Context API With Class And Functional Componen in React.pptxHow To Utilize Context API With Class And Functional Componen in React.pptx
How To Utilize Context API With Class And Functional Componen in React.pptx
 
A full introductory guide to React
A full introductory guide to ReactA full introductory guide to React
A full introductory guide to React
 
React + Redux + TypeScript === ♥
React + Redux + TypeScript === ♥React + Redux + TypeScript === ♥
React + Redux + TypeScript === ♥
 
Adding a modern twist to legacy web applications
Adding a modern twist to legacy web applicationsAdding a modern twist to legacy web applications
Adding a modern twist to legacy web applications
 
ReactJS presentation.pptx
ReactJS presentation.pptxReactJS presentation.pptx
ReactJS presentation.pptx
 
react-slides.pdf
react-slides.pdfreact-slides.pdf
react-slides.pdf
 

Intro react js

  • 1. ReactJS an Intro Vijayakanth M DevOps Engineer Megam Systems 1
  • 2. AGENDA 2 1. What is ReactJS and Why ? 2. ReactJS with JSX 3. Lifecycle Methods (specs and state) 4. Some code 5. Installation and Config
  • 3. 3 ❖ ReactJS is a JavaScript Library for creating user interfaces by Facebook and Instagram. ❖ ReactJS is a building block of Components. ❖ In ReactJS, application data flows unidirectionally via the state and props objects, contrast to frameworks like Angular and Ember. ❖ ReactJS runs a “diffing” algorithm, which identifies that has changed. ❖ This will serve purely as an API endpoint which we'll use for getting and saving data. What is ReactJS ?
  • 4. 4 Flow of ReactJS MVC Process ReactJS Flux Process
  • 5. Why do we use ReactJS ? ❖ ReactJS is extremely fast, because it uses virtual DOM, and syncs only the changed parts. ❖ React.js uses a special syntax called JSX, which allows you to mix HTML with JavaScript. ❖ When the data changes, ReactJS conceptually hits the "refresh" button, and knows to only update the changed parts. ❖ React is all about building reusable components. ❖ It is much smaller and plays nicely with jQuery and other frameworks.
  • 6. 6 ReactJS with JSX ❖ JSX is a JavaScript syntax extension that looks similar to XML. You can use a simple JSX syntactic transform with React. ❖ React JSX transforms from an XML-like syntax into native JavaScript. var Nav, Profile; // Input (JSX): var app = <Nav color="blue"><Profile>click</Profile></Nav>; // Output (JS): var app = React.createElement( Nav, {color:"blue"}, React.createElement(Profile, null, "click") );
  • 7. 7 Now see a simple structure of ReactJS Component var MyComponent = React.createClass({ render: function(){ return ( <h1>Hello, world!</h1> <CommentList /> );} }); How to call the Components in Rails <% react_component ‘MyComponent’ %> var CommentList = React.createClass({ render: function() { return ( <div className="commentList"> <h1> I am a CommentList. </h1> </div> ); } }); Every Component must have a render function, it will return an another react Component or tags
  • 8. 8 Lifecycle Methods, Specs and State 8 componentWillMount Invoked once, on both client & server before rendering occurs. componentDidMount Invoked once, only on the client, after rendering occurs. shouldComponentUpdate Return value determines whether component should update componentWillUnmount Invoked prior to unmounting component.
  • 9. 9 continue…. State Every component has a state object and a props object. State is set using the setState method. Calling setState triggers UI updates and is the bread and butter of React’s interactivity. If we want to set an initial state before any interaction occurs we can use the getInitialState method. getInitialState Return value is the initial value for state. getDefaultProps Sets fallback props values if props aren’t supplied. Specs
  • 10. 10 some code //Child component var Comment = React.createClass({ render: function() { return ( <div className="comment"> <h2 className="commentAuthor"> {this.props.author} </h2> {this.props.children} </div> ); } }); //Parent component var CommentList = React.createClass({ render: function() { return ( <div className="commentList"> <Comment author="Pete Hunt">This is one comment</Comment> <Comment author="Jordan Walke">This is *another* comment</Comment> </div> ); } }); <%= react_component ‘CommentList’ %> Note that we have passed some data from the parent CommentList component to the child Comment components. For example, we passed PeteHunt (via an attribute) and This is one comment (via an XML- like child node) to the first Comment. As noted above, the Comment component will access these 'properties' through this.props.author, and this.props.children.
  • 11. 11 var data = [ {author: "Pete Hunt", text: "This is one comment"},{},...} ]; var CommentBox = React.createClass({ loadCommentsFromServer: function() { $.ajax({ //… Make a ajax call to get data from server }); }, getInitialState: function() { return {data: []}; }, // to get data continuous componentDidMount: function() { this.loadCommentsFromServer(); setInterval(this.loadCommentsFromServer, this.props.pollInterval); }, // Render another Components render: function() { return ( <div className="commentBox"> <h1>Comments</h1> <CommentList data={this.state.data} /> </div> ); } }); Data from the server
  • 12. 12 continue... // To list all data get from the server var CommentList = React.createClass({ render: function() { var commentNodes = this.props.data.map(function (comment) { return ( <Comment author={comment.author}> {comment.text} </Comment> ); }); return ( <div className="commentList"> {commentNodes} </div> ); } var Comment = React.createClass({ render: function() { return ( <div className="comment"> <h2 className="commentAuthor"> {this.props.author} </h2> {this.props.children} </div> ); } });
  • 13. 13 continue... <%= react_component ‘CommentBox’ url: “/json”, timeInterval: 4000 %> Or <script type="text/javascript"> ReactDOM.render( ReactDOM.render(React.createElement(NewOverview, { url: “/json”, timeInterval:4000}), document.getElementById('content') ); </script> Write the Component code in : app/assets/javascripts/components/<filename>.js.jsx the calling command on: app/views/<directory name>/<htmlerbfile>.html.erb The flow of the Program
  • 14. 14 Flow of above Components CommentBox //Parent component getInitialState { } loadCommentsFromServer{ $.ajax({ //… Make a ajax call to get data }); }, componentDidMount {setInterval( Get synchronous ajax call to get data) } CommentList //Child component Map{ // fetch each data from whole json and send to component } Comment //Child component render: { return ( Call children components and send data)} render: { return ( call Comment component)} render: { return ( Show all data )}
  • 15. 15 continue... Events React attaches event handlers(onSubmit) to components using a camelCase naming convention. Call preventDefault() on the event to prevent the browser's default action of submitting the form. Refs We use the ref attribute to assign a name to a child component and this.refs to reference the component. We can call React.findDOMNode(component) on a component to get the native browser DOM element. Callbacks as props We need to pass data from the child component back up to its parent. We do this in our parent's render method by passing a new callback ( handleCommentSubmit) into the child, binding it to the child's onCommentSubmit event. Whenever the event is triggered, the callback will be invoked:
  • 16. 16 Installation and Config Add react-rails to your gemfile: gem 'react-rails', '~> 1.3.0' Next, run the installation script: rails g react:install place the following in your application.js(app/assets/javascripts/): //= require react //= require react_ujs //= require components You can pick which React.js build (development, production, with or without add-ons) to serve in each environment by adding a config. # config/environments/development.rb MyApp::Application.configure do config.react.variant = :development end # config/environments/production.rb MyApp::Application.configure do config.react.variant = :production end
  • 18. 18 Vijayakanth M DevOps Engineer Megam Systems (www.megam.io) Twitter: @vijayakanth_mp Email: mvijaykanth@megam.io Docs: docs.megam.io Devcenter: Devcenter.megam.io Thank you…