React Design Patterns and Best Practices by Carlos Santana Roldán
Author:Carlos Santana Roldán
Language: eng
Format: epub
Tags: COM051260 - COMPUTERS / Programming Languages / JavaScript, COM060080 - COMPUTERS / Web / General, COM060160 - COMPUTERS / Web / Web Programming
Publisher: Packt Publishing
Published: 2019-03-29T13:49:17+00:00
this.handleEvent = this.handleEvent.bind(this);
}
Second, we implement the generic event handler:
handleEvent(event) {
switch (event.type) {
case 'click':
console.log('clicked');
break;
case 'dblclick':
console.log('double clicked');
break;
default:
console.log('unhandled', event.type);
}
}
The generic event handler receives the event object and switches on the event type to fire the right action. This is particularly useful if we want to call a function on each event (for example, analytics) or if some events share the same logic.
Finally, we attach the new event listener to the onClick and onDoubleClick attributes:
render() {
return (
<button
onClick={this.handleEvent}
onDoubleClick={this.handleEvent}
>
Click me!
</button>
);
}
From this point on, whenever we need to create a new event handler for the same component, instead of creating a new method and binding it, we can just add a new case to the switch.
A couple more interesting things to know about events in React are that synthetic events are reused and that there is a single global handler. The first concept means that we cannot store a synthetic event and reuse it later because it becomes null right after the action. This technique is very good in terms of performance, but it can be problematic if we want to store the event inside the state of the component for some reason. To solve this problem, React gives us a persist method on the synthetic events, which we can call to make the event persistent so that we can store it and retrieve it later.
The second very interesting implementation detail is again about performance, and it regards the way React attaches the event handlers to the DOM.
Whenever we use the on attributes, we are describing to React the behavior we want to achieve, but the library does not attach the actual event handler to the underlying DOM nodes.
What it does instead is attach a single event handler to the root element, which listens to all the events, thanks to event bubbling. When an event we are interested in is fired by the browser, React calls the handler on the specific components on its behalf. This technique is called event delegation and is used for memory and speed optimization.
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Sass and Compass in Action by Wynn Netherland Nathan Weizenbaum Chris Eppstein Brandon Mathis(7775)
Grails in Action by Glen Smith Peter Ledbrook(7693)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(6409)
Kotlin in Action by Dmitry Jemerov(5062)
WordPress Plugin Development Cookbook by Yannick Lefebvre(3773)
Mastering Azure Security by Mustafa Toroman and Tom Janetscheck(3327)
Learning React: Functional Web Development with React and Redux by Banks Alex & Porcello Eve(3085)
Mastering Bitcoin: Programming the Open Blockchain by Andreas M. Antonopoulos(2867)
The Art Of Deception by Kevin Mitnick(2602)
Drugs Unlimited by Mike Power(2465)
Kali Linux - An Ethical Hacker's Cookbook: End-to-end penetration testing solutions by Sharma Himanshu(2310)
The Innovators: How a Group of Hackers, Geniuses, and Geeks Created the Digital Revolution by Walter Isaacson(2297)
Writing for the Web: Creating Compelling Web Content Using Words, Pictures and Sound (Eva Spring's Library) by Lynda Felder(2261)
SEO 2018: Learn search engine optimization with smart internet marketing strategies by Adam Clarke(2190)
JavaScript by Example by S Dani Akash(2134)
A Blueprint for Production-Ready Web Applications: Leverage industry best practices to create complete web apps with Python, TypeScript, and AWS by Dr. Philip Jones(2124)
DarkMarket by Misha Glenny(2083)
Wireless Hacking 101 by Karina Astudillo(2075)
Full-Stack React Projects by Shama Hoque(1990)
