https://www.sublimetext.com/
縮排控制
上色語言
CTRL+F | 搜尋 |
CTRL+H | 取代 |
CTRL+Shift+P | 開啟指令介面 |
Esc | 離開目前功能 |
CTRL+Click | multiple selection |
https://www.sublimemerge.com/
HyperText Markup Language
超文字
標記
語言
<TAGNAME>EXAMPLE</TAGNAME>
<html>
<body>
<h1>My First Heading</h1>
<p>My first paragraph</p>
</body>
</html>
<TAGNAME>
<img src="https://user-images.githubusercontent.com/30186107/29488525-f55a69d0-84da-11e7-8a39-5476f663b5eb.png" />
<input />
<br />
<a href="https://www.w3schools.com/html/default.asp" />
<link />
<meta />
<!DOCTYPE>
<h1>heading1</h1>
<h2>heading2</h2>
<h3>heading3</h3>
<h4>heading4</h4>
<h5>heading5</h5>
<h6>heading6</h6>
<p>paragraph</p>
<!DOCTYPE>
<h1 style="color: red;">heading1</h1>
<h2 style="color: orange;">heading2</h2>
<h3 style="color: yellow;">heading3</h3>
<h4 style="color: green;">heading4</h4>
<h5 style="color: blue;">heading5</h5>
<h6 style="color: purple;">heading6</h6>
<p style="color: #808080;">paragraph</p>
<TAGNAME ATTRIBUTE="VALUE"></TAGNAME>
<!DOCTYPE>
<h1 style="color: red;text-align: center;border-style: solid;">heading1</h1>
<h2 style="color: orange;text-align: center;border-style: solid;">heading2</h2>
<h3 style="color: yellow;text-align: center;border-style: solid;">heading3</h3>
<h4 style="color: green;text-align: center;border-style: solid;">heading4</h4>
<h5 style="color: blue;text-align: center;border-style: solid;">heading5</h5>
<h6 style="color: purple;text-align: center;border-style: solid;">heading6</h6>
<p style="color: gray;text-align: center;border-style: solid;">paragraph</p>
<!DOCTYPE>
<style>
.text{
text-align: center;
border-style: solid;
}
.round{
border-radius: 100px;
}
</style>
<h1 class="text round" style="color: red;">heading1</h1>
<h2 class="text" style="color: orange;">heading2</h2>
<h3 class="text round" style="color: yellow;">heading3</h3>
<h4 class="text" style="color: green;">heading4</h4>
<h5 class="text round" style="color: blue;">heading5</h5>
<h6 class="text" style="color: purple;">heading6</h6>
<p class="text round" style="color: gray;">paragraph</p>
Cascading Style Sheets
/* select by element name */
h1 {font-weight: 1000;}
/* select by class */
.center {text-align: center;}
/* select by id */
#box {background-color: red;}
/* select all */
* {font-family: 'Noto Sans TC';}
print用console.log
任何東西都是Object
函式宣告用function NAME(ARGS){}
簡化版function宣告(類似lambda function)
const func = in => out;
const square = x => x * x;
const func = (a, b, c) => {
a += b;
return a + c;
}
function func(a, b, c){
a += b;
return a + c;
}
document.getElementById
document.getElementsByTag
document.getElementsByClassName
<h1 id="test">Testing Page</h1>
<script>
let ele = document.getElementById('test');
ele.innerHTML = `
<span style="color: red">
RED TEXT
</span>
`;
</script>
<button onclick="add()">Add</button>
<ol id="list"></ol>
<script>
function add(){
const ele = document.getElementById('list');
ele.innerHTML += `<li>item</li>`;
}
</script>
<button id="add">Add</button>
<ol id="list"></ol>
<script>
function add(){
const ele = document.getElementById('list');
ele.innerHTML += `<li>item</li>`;
}
document.getElementById('add')
.addEventListener('click', add);
</script>
npx create-react-app my-app
cd my-app
npm start
node_modules/ | 套件安裝的目的(不會動到) |
public/ | meta data(不會動到) |
src/ | 網頁主體 |
package.json | 管理套件的檔案 |
function App() {
return (
<>
<h1>My First Heading</h1>
<p>My first paragraph</p>
</>
);
}
export default App;
function App() {
const name = 'INFOR';
return (
<>
<h1>Hello {name}</h1>
</>
);
}
export default App;
function App() {
const greeting = <h1>Hello INFOR</h1>;
return (
<>
<ol>
<li>{greeting}</li>
</ol>
</>
);
}
export default App;
function App() {
const people = [
<li>thomaswang</li>,
<li>casperwang</li>,
<li>ericxiao</li>,
];
return (
<>
<ol>
{people}
</ol>
</>
);
}
export default App;
function App() {
const people = [
'thomaswang',
'casperwang',
'ericxiao',
];
return (
<>
<ol>
{people.map(name => <li key={name}>{name}</li>)}
</ol>
</>
);
}
export default App;
without key
with key
1,2,3 \(\longrightarrow\) 2,3,4
function App() {
const people = [
'thomaswang',
'casperwang',
'ericxiao',
];
return (
<>
<ol style={{ color: 'red' }}>
{people.map(name => <li>{name}</li>)}
</ol>
</>
);
}
export default App;
<div style="color: #fff; font-size: 20px;">
<div style={{ color: '#fff', fontSize: '20px' }}>
flex-direction
text-align
flexDirection
textAlign
function Button() {
return (
<button>click me</button>
);
}
function App() {
return (
<>
<Button />
<Button />
</>
);
}
export default App;
function Button({ name }) {
return (
<button>{name}: click me</button>
);
}
function App() {
return (
<>
<Button name="A" />
<Button name="B" />
</>
);
}
export default App;
function Button({ children }) {
return (
<button>{children}</button>
);
}
function App() {
return (
<>
<Button>A</Button>
<Button>B</Button>
</>
);
}
export default App;
function Button({ children }) {
return (
<button onClick={() => console.log(children)}>{children}</button>
);
}
function App() {
return (
<>
<Button>A</Button>
<Button>B</Button>
</>
);
}
export default App;
寫程式的精神:減少重複!
function Button({ children }) {
return (
<button style={{
background: 'red',
cursor: 'pointer',
}} onClick={() => console.log(children)}>
{children}
</button>
);
}
function App() {
const btns = [
'btn-1',
'btn-2',
'btn-3',
'btn-4',
'btn-5',
'btn-6',
'btn-7',
'btn-8',
]
return (
<>
{btns.map(x => <Button key={x}>{x}</Button>)}
</>
);
}
export default App;
<button
style="background: red;cursor: pointer;"
onclick="console.log('btn-1')">
btn-1
</button>
<button
style="background: red;cursor: pointer;"
onclick="console.log('btn-2')">
btn-2
</button>
<button
style="background: red;cursor: pointer;"
onclick="console.log('btn-3')">
btn-3
</button>
<button
style="background: red;cursor: pointer;"
onclick="console.log('btn-4')">
btn-4
</button>
<button
style="background: red;cursor: pointer;"
onclick="console.log('btn-5')">
btn-5
</button>
<button
style="background: red;cursor: pointer;"
onclick="console.log('btn-6')">
btn-6
</button>
<button
style="background: red;cursor: pointer;"
onclick="console.log('btn-7')">
btn-7
</button>
<button
style="background: red;cursor: pointer;"
onclick="console.log('btn-8')">
btn-8
</button>
以上,我們簡化了排版部分
<h1 id="counter">0</h1>
<button onclick="add()">++</button>
<script>
let cnt = 0;
function add(){
cnt++;
document.getElementById('counter').innerText = cnt;
}
</script>
cnt其實就是state
缺點:state改變後要手動改變畫面(line 7)
import { useState } from 'react';
function App() {
const [cnt, setCnt] = useState(0);
function add(){
setCnt(cnt + 1);
}
return (
<>
<h1>{cnt}</h1>
<button onClick={add}>++</button>
</>
);
}
export default App;
cnt真的就是state
state改變後會直接反應在畫面上
Simple Design, High Efficiency
useEffect // making change to non-state things
useMemo // states computed from other states
useCallback // specialized useMemo for function
react-router-dom // handle route (url) change
context // global state
npm start開的是開發伺服器(development server)
開發伺服器並不適合做為產品
事實上我們做的都是靜態網頁,而靜態網頁不需要伺服器
要將所有react設計編譯、最佳化成靜態檔案
npm run build
node.js 後端套件
function resolveAfter2Seconds() {
return new Promise(resolve => {
setTimeout(() => {
resolve('resolved');
}, 2000);
});
}
async function asyncCall() {
console.log('calling');
const result = await resolveAfter2Seconds();
console.log(result);
}
console.log('before');
asyncCall();
console.log('after?');
async function asyncCall() {
console.log('calling');
let res = await fetch('http://localhost:5000');
res = await res.text();
console.log(`localhost:5000 says: ${res}`);
}
console.log('before');
asyncCall();
console.log('after?');
npm install express
node index.js
const express = require('express');
const app = express();
const port = 5000;
app.get('/', (req, res) => {
res.send('Hello World!');
});
app.listen(port, () => {
console.log(`listening on port ${port}`);
});
app.get('/:name', (req, res) => {
res.send(`Hello ${req.params.name}!`);
});
cnt = {};
app.get('/:name', (req, res) => {
const name = req.params.name;
if(cnt[name] === undefined)
cnt[name] = 0;
res.send(cnt[name].toString());
});
cnt = {};
app.get('/:name', (req, res) => {
const { name } = req.params;
if(!(name in cnt))
cnt[name] = 0;
res.send(`${cnt[name]}`);
});
app.post('/add', (req, res) => {
const { name } = req.body;
cnt[name]++;
res.send(cnt[name].toString());
});
app.post('/add', (req, res) => {
const { name } = req.body;
if(!(name in cnt))
return res.sendStatus(400);
cnt[name]++;
res.send(cnt[name].toString());
});
import { useState, useEffect } from 'react';
function App() {
const [cnt, setCnt] = useState(false);
const [name, setName] = useState("");
const [value, setValue] = useState("");
async function add(){
let res = await fetch(`http://localhost:5000/add`, {
method: 'POST',
body: JSON.stringify({ name: name }),
headers: {
'Content-Type': 'application/json',
}
});
res = await res.text();
setCnt(Number(res));
}
function change(event){
setValue(event.target.value);
}
useEffect(() => {
if(name.length === 0) return;
(async () => {
let res = await fetch(`http://localhost:5000/${name}`);
res = await res.text();
setCnt(Number(res));
})();
}, [name]);
return (
<>
<input type="text" value={value} onChange={change}/>
<button onClick={() => setName(value)}>OK</button>
<p>
{cnt !== false ?
<>
{cnt}<button onClick={add}>++</button>
</> : ""}
</p>
</>
);
}
export default App;
import { useState, useEffect } from 'react';
function App() {
const [cnt, setCnt] = useState(false);
const [value, setValue] = useState("");
async function add(){
let res = await fetch(`http://localhost:5000/add`, {
method: 'POST',
body: JSON.stringify({ name: value }),
headers: {
'Content-Type': 'application/json',
}
});
res = await res.text();
setCnt(Number(res));
}
function change(event){
setValue(event.target.value);
setCnt(false);
}
useEffect(() => {
if(value.length === 0){
setCnt(false);
return;
}
const id = setTimeout(async () => {
let res = await fetch(`http://localhost:5000/${value}`);
res = await res.text();
setCnt(Number(res));
}, 500);
return () => clearTimeout(id);
}, [value]);
return (
<>
<input type="text" value={value} onChange={change}/>
<p>
{cnt !== false ?
<>
{cnt}<button onClick={add}>++</button>
</> : ""}
</p>
</>
);
}
export default App;
npm install fs // access file system