Reactjs en Events
Reactjs en Events
Fundamentals
About me
SARRAJ Khaireddine
Twitter: twitter.com/Acewings_sk
LinkedIn: linkedin.com/in/khaireddinesarraj-3220a1150
WWW: fiverr.com/share/NEwv0o
What is React js?
What is React js?
• Developed by Facebook
• React is a view layer library, not a framework like Backbone, Angular etc.
• JavaScript is fast.
• Using virtual DOM*1 objects enables fast batch updates to real DOM, with great productivity gains over
frequent cascading updates of DOM tree.
• Server-side rendering
• React.renderToString() returns pure HTML.
1*The virtual DOM (VDOM) is a programming concept where an ideal, or “virtual”, representation of a UI is kept in memory and synced with the
“real” DOM by a library such as ReactDOM
The Cons of using React js?
• React is nothing but the view • No support for older browsers
• No events • React won't work with IE8
• No XHR
• There some polyfills / shims that help
• No data / models
• No idea how to add all of the above • But most of the work is already done for you
• Architectural annoyances
• No pre-defined good practice directory schema to
follow
Why should we
React js?
Why should we React js?
• Easy to read and understand views
• If your page uses a lot of fast updating data or real time data – React is the way
to go
• Total Freedom on whatever you want to do and implements – you can go wild !!
Fundamentals
Most important terms in React?
Components
React Components
• Components are self-contained • React components implement a render() method that
reusable building blocks of web takes input data and returns what to display. This
example uses an XML-like syntax called JSX. Input data
application.
that is passed into the component can be accessed by
• React components are basically render() via this.props
just idempotent functions (same
input produces same output).
• Accessed via this.state directly. Instead, state updates should be made with a special
method called setState( ).
• When a component's state data changes, the rendered
markup will be updated by re-invoking render() method
• If...else statement
• Creating an Array dynamically with the help of Array.From() Method out of:
which it is called.
values.
Handling Events
React Event handling
• Handling events with React elements is very similar to handling
events on DOM elements. There are some syntax differences: Events
always start with ”on<event>”, for example onClick, onMouseEnter,
onMouseLeave, onScroll, onDragStart, onDragStop, ….
• With JSX you pass a function as the event handler, rather than a
string.
• The above two lines are equivalent, and use arrow functions and Function.prototype.bind respectively..
• In both cases, the e argument representing the React event will be passed as a second argument after the ID. With an
arrow function, we have to pass it explicitly, but with bind any further arguments are automatically forwarded.
Synthetic Events
• Your event handlers will be passed instances of SyntheticEvent, a cross-browser wrapper around the browser’s native
event. It has the same interface as the browser’s native event, including stopPropagation() and preventDefault(),
• When using React, you generally don’t need to call addEventListener to add listeners to a DOM element after it is
created. Instead, just provide a listener when the element is initially rendered.