createStore
createStore
Store
sẽ chứa các state
- dữ liệu trạng thái
.store
sẽ là reducer
, xem như reducer là 1 callback
sẽ viết sau này để truyền vào.new state
sẽ được tạo từ xử lý reducer
, nên ta khai báo như sau:function createStore(reducer){
let state = reducer()
...
}
VIEW
.html
để truyền vào element có id = "root"
.function html([first, ...values], ...strings) {
return values
.reduce(
(acc, cur) => {
return acc.concat(strings.shift(), cur);
},
[first]
)
.filter((x) => (x && x !== true) || x === 0)
.join('');
}
id = "root"
được gọi là root (Node gốc), và sẽ được render thành chuỗi stringHtml để truyền vào DOM thông qua innerHtml.function createStore(reducer){
let state = reducer()
const roots = new Map(); // Ban đầu thì roots sẽ không có dữ liệu
/**
* roots : Map(){
* key : Value
* div#root : () => `<h1>Hello World!!!</h1>`
* root: div#root
* component : () => `<h1>Hello World!!!</h1>`
* }
*/
// attach(() => `<h1>Hello World!!!</h1>`, document.getElementById('root'));
/**
* roots : Map(1) {div#root => ƒ}
* {div#root => () => `<h1>Hello World!!!</h1>`}
* key: div#root
* value: () => `<h1>Hello World!!!</h1>`
* [[Prototype]]: Map
*/
...
}
key
sẽ là element root
value
sẽ là một hàm component
để tạo ra được chuỗi html
, ta sẽ gọi là thành phần component
.roots : Map(){
key : Value
//Ví dụ: div#root : () => `<h1>Hello World!!!</h1>`
//Trong đó root: div#root
//component : () => `<h1>Hello World!!!</h1>`
}
Map(){
// key : Value
div#root : () => `<h1>Hello World!!!</h1>`
}
Thì:
key
sẽ là div#root
component
sẽ là () => '<h1>Hello World!!!</h1>'
Khi này hàm createStore sẽ là :
function createStore(reducer) {
let state = reducer();
const roots = new Map(); // Ban đầu thì roots sẽ không có dữ liệu
}