Translation | "JavaScript Everywhere" Chapter 11 User Interface and React ( _ )
Write at the top
Hello everyone, I am Mao Xiaoyou, a front-end development engineer. An English technical book is being translated.
In order to improve everyone's reading experience, the structure and content of sentences have been slightly adjusted. If you find any flaws in this article, or if you have any comments or suggestions, you can leave a message in the comment area, or add my WeChat: code_maomao, welcome to communicate and learn from each other.
(σ゚∀゚)σ…:*☆Oh, good
Chapter 11 User Interface and React
1979
In 2009, the famous Steve Jobs ( Steve Jobs
) visited Xerox Parker ( Xerox Parc
), where he saw a Alto
demonstration of Xerox's personal computer. At that time, other computers were controlled by typing commands, and Alto
they used a mouse and had a graphical window interface that could be opened and closed. Jobs Apple Macintosh
continued to draw on these ideas in his original creation. The original Mac
popularity led to UI
a proliferation of computers . Today, on a typical day, we may interact with many graphical user interfaces, which may include personal computers as well as smartphones, tablets, ATM
and game consoles. The user interface now surrounds us and can be used across various devices, content types, screen sizes, and interactive formats.
For example, I recently went to another city for a meeting. That morning, I woke up and checked the flight status on my phone. I drove to the airport and showed a map on the screen, allowing me to choose the music I was listening to. On the way, I stopped on the ATM
plane to retrieve some cash, entered the PIN
code and entered the instructions on the touch screen. When I arrived at the airport, I boarded at a check-in counter. While waiting for the bus at the boarding gate, I responded to several emails on my tablet. During the flight, I read a book on the electronic ink display device. After landing, I used an app on my phone to find a car to ride, then stopped for lunch, and selected my order on the display. At the meeting, a slide was projected onto the screen platform, and many of us took notes on laptops. When I returned to my hotel in the evening, through the hotel TV screen menu, I browsed the TV and movie products I found. My day is full of many UI
screen sizes, used to complete tasks related to the core elements of life (such as transportation, finance, and entertainment).
In this chapter, we will briefly introduce JavaScript
the history of user interface development. After mastering this background knowledge, we will explore React
the basics and JavaScript
libraries that will be used in the rest of this book .
JavaScript sum UI
It was originally designed in 1990
the middle of the decade (not well-known, 10 days ) to enhance the Web
interface and provide an embedded scripting language JavaScript
in the Web
browser. This allows Web
designers and developers to add HTML
interactions that cannot be achieved by themselves . Unfortunately, each browser vendor has a different JavaScript
implementation, so it is difficult to use. This is one of the factors leading to the proliferation of applications designed to work in a single browser.
In 2000
the middle of the decade
jQuery
(And similar libraries, for example MooTools
) became popular. jQuery
Allow developers to use simple API
writing JavaScript
, which API
can run well between various browsers. Soon, we can all delete, add, replace and animate the content on the webpage. Around the same time, Ajax
( abbreviation for "asynchronous JavaScript
sum XML
") enabled us to fetch data from the server and inject it into the page. The combination of these two technologies Web
provides an ecosystem for creating powerful interactive applications.
As the complexity of these applications increases, so does the need for organization and sample code. By 2010
the beginning of the decade, frameworks such as Backbone
, Angular
and Ember
began to dominate the JavaScript
application domain. These frameworks work by imposing structure in the framework code and implementing common application patterns. These frameworks are usually MVC
modeled according to the software design module, view, and controller ( ) mode. Each framework Web
regulates all layers of the application and provides a structured way to handle templates, data, and user interaction. Although this has many benefits, it also means that the workload of integrating new or non-standard technologies may be large.
At the same time, desktop applications continue to be written in system-specific programming languages. This means that developers and teams are often forced to make one or two style choices ( Mac
application or Windows
application, Web
application or desktop application, etc.). The mobile application is in a similar position. Web
The rise of responsive design means that designers and developers can Web
create truly incredible websites and applications for mobile browsers, but choosing to build only-based Web
applications locks them out of the mobile platform application repository. Apple
'S iOS
applications are written in Objective C
(and more recently Swift
) and Android
rely on Java
programming languages (not to JavaScript
be confused with our friends ). This means that by the HTML
, CSS
and JavaScript
the composition Web
is the only true cross-platform user interface platform.
JavaScript's declarative interface
In 2010
the early years, Facebook
developers JavaScript
faced challenges in the organization and management of their code. In response, a software engineer Jordan Walke
in Facebook
the PHP
library XHP
inspired to write next React
. The difference React
from other popular JavaSript
frameworks is that it only focuses on UI
presentation. To this end, React
a "declarative" programming approach is adopted, which means that it provides an abstraction that allows developers to focus on the described UI
state.
With the rise of React
and similar libraries (for example Vue.js
), we have seen UI
changes in the way developers write . These frameworks provide a way to manage UI
state at the component level . This makes the user feel smooth about the application, while providing an excellent development experience. With tools such as those for building desktop applications Electron
and those for cross-platform native mobile applications React Native
, developers and teams can now leverage these paradigms in all their applications.
React is enough
In the remaining chapters, we will rely on React
libraries to build ours UI
. You don't need to have any React
experience in using it, but before using it, if you have some understanding of the grammar, it will be helpful to use it. For this, we will use to create-react-app
build a new project. create-react-app
It is React
a tool developed by the team, which allows us to quickly build a new React
project and help abstract the underlying build tools, such as Webpack
and Babel
.
cd
Enter the project directory in the terminal application and run the following command, which will just-enough-react
create a new React
application in the folder named :
$ npx create-react-app just-enough-react
$ cd just-enough-react
Running these commands will just-enough-react
output a directory in which contains all the project structure, code dependencies and development scripts used to build a fully functional application. Start the application by running the following command:
$ npm start
Now our React
application will be http://localhost:3000
visible on the browser (picture 11-1
).
Picture 11-1
input npm start
will start the default in the browsercreate-react-app
Now, we can src/App.js
start editing our application by changing the file. This file contains our main React
components. After some dependencies are required, it contains a HTML
function that returns some tokens similar to :
function App() {
return (
// markup is here
)
}
The mark used in the component is called JSX
.
JSX
Is a similar XML
-based XML
grammar that allows us to accurately describe UI
and combine it with JavaScript
user actions in the file. If you understand HTML
, the choice JSX
is just to learn some small differences. The biggest difference in this example is HTML
that the class
attributes are className
replaced to avoid JavaScript
conflicts with the class syntax.
JSX?
If, like me, you come from the background of network standardization and strict separation of concerns, then you may find this annoying. I admit that JSX
when I first encountered it, I immediately strongly disliked it. However, UI
the coupling of logic and presentation output provides a number of compelling advantages that may grow over time.
Let's Hello World
start customizing our application by removing most of the style code and simplifying it to a simple " !":
import React from 'react';
import './App.css';
function App() {
return (
<div className="App">
<p>Hello world!</p>
</div>
);
}
export default App;
You may notice JSX
the tags that encapsulate all of our content. Each React UI
component must be contained within a parent HTML
element or use a React
fragment, which represents a non- HTML
element container, for example:
function App() {
return (
<React.Fragment>
<p>Hello world!</p>
</React.Fragment>
);
}
About React
one of the most powerful features is that we can be enclosed in braces {} is directly in JSX
use JavaScript
. Let's update the App
function to use some variables:
function App() {
const name = 'Adam'
const now = String(new Date())
return (
<div className="App">
<p>Hello {
name}!</p>
<p>The current time is {
now}</p>
<p>Two plus two is {
2+2}</p>
</div>
);
}
In the previous example, you can see that we used it directly in the interface JavaScript
. How cool is it?
React
Another useful feature is the ability to turn each UI
function into its own component. A good rule of thumb is that if UI
one aspect of it operates in an independent manner, it should be separated into its own components. Let's create a new component. First, src/Sparkle.js
create a new file in and declare a new function:
import React from 'react';
function Sparkle() {
return (
<div>
</div>
);
}
export default Sparkle;
Now let's add some features. Whenever the user clicks a button, it will add a flashing emoji (a key feature of any application) to our page. In order to do this, we will import React
the useState
component and define some initial state for the component, the initial state will be an empty string (in other words, no flicker).
import React, {
useState } from 'react';
function Sparkle() {
// declare our initial component state
// this a variable of 'sparkle' which is an empty string
// we've also defined an 'addSparkle' function, which
// we'll call in our click handler
const [sparkle, addSparkle] = useState('');
return (
<div>
<p>{
sparkle}</p>
</div>
);
}
export default Sparkle;
What is State?
We will 15
introduce more details in Chapter 1 state
, but for now, you only need to know that stateful components can change the current state of any information in the component.
For example, if a UI
component has a check box, its state is when it is selected and its state is when it is true
not false
.
Now, we can onClick
complete the component by adding functional buttons. Note the camel case naming, which JSX
is required in:
import React, {
useState } from 'react';
function Sparkle() {
// declare our initial component state
// this a variable of 'sparkle' which is an empty string
// we've also defined an 'addSparkle' function, which
// we'll call in our click handler
const [sparkle, addSparkle] = useState('');
return (
<div>
<button onClick={
() => addSparkle(sparkle + '\u2728')}>
Add some sparkle
</button>
<p>{
sparkle}</p>
</div>
);
}
export default Sparkle;
To use our component, we can import it into a src/App.js
file and declare it as an JSX
element as follows:
import React from 'react';
import './App.css';
// import our Sparkle component
import Sparkle from './Sparkle'
function App() {
const name = 'Adam';
let now = String(new Date());
return (
<div className="App">
<p>Hello {
name}!</p>
<p>The current time is {
now}</p>
<p>Two plus two is {
2+2}</p>
<Sparkle />
</div>
);
}
export default App;
Now, if you visit our app in a browser, you should see our button and be able to click it to add the sparkling emoji to the page! This is one React
of the real super powers. We are able to re-render individual components or components (pictures 11-2
) independently of the rest of the application .
Figure 11-2
Clicking the button will update the component state and add the content to our page
Now, we have create-react-app
created a new application, updated the application components JSX
, created a new component, declared the component state, and dynamically updated the component. With a basic understanding of these basics, we are now ready to React
use JavaScript
declarative development UI
.
in conclusion
We are surrounded by user interfaces on various devices. JavaScript
And Web
technology provides unparalleled opportunities to develop these interfaces on multiple platforms using a single technology. At the same time, React
and other declarative visual libraries enable us to build powerful dynamic applications. The combination of these technologies allows developers to build amazing things without having to have expertise on each platform. In the following chapters, we will put it into practice by using the interfaces GraphQL API
built for Web
, desktop, and mobile applications.
If there is any inadequate understanding, please correct me. If you think it's okay, please like to collect or share it, hoping to help more people.