这个系列的目的是通过使用 JS 实现“乞丐版”的 React,让读者了解 React 的基本工作原理,体会 React 带来的构建应用的优势

使用 HTML 和 CSS,我们很容易可以构建出上图中的页面
<!DOCTYPE html>
<html lang="en">
<head>
<title>Build my react</title>
<style>
div {
text-align: center;
}
.father {
display: flex;
flex-direction: column;
justify-content: center;
height: 500px;
background-color: #282c34;
font-size: 30px;
font-weight: 700;
color: #61dafb;
}
.child {
color: #fff;
font-size: 16px;
font-weight: 200;
}
</style>
</head>
<body>
<div class="father">
Fucking React
<div class="child">用于构建用户界面的 JavaScript 库</div>
</div>
</body>
</html>
当然这只是一个静态的页面,我们知道,网站中最重要的活动之一是和用户产生交互,用户通过触发事件来让网页产生变化,这时就需要用到 JS
使用 DOM 操作,我们也可以构建上面的静态页面,并且可以动态地改变页面、添加事件监听等来让网页活动变得更加丰富
我们先改写一下 HTML 的 body(如果没有特殊说明,本文不会更改 CSS 的内容),我们将 body 中的内容都去掉,新增一个 id 为 root 都 div 标签,并且引入index.js。
<div id="root"></div>
<script src="./index.js"></script>
index.js内容如下:
const text = document.createTextNode("Fucking React");
const childText = document.createTextNode("用于构建用户界面的 JavaScript 库");
const child = document.createElement("div");
child.className = "child";
child.appendChild(childText);
const father = document.createElement("div");
father.className = "father";
father.appendChild(text);
father.appendChild(child);
const container = document.getElementById("root");
container.appendChild(father);
使用 DOM 操作,我们也可以构建出同样的页面内容,但是缺点很明显
<div class="father">
Fucking React
<div class="child">用于构建用户界面的 JavaScript 库</div>
</div>
原本只要寥寥几行 HTML 的页面。使用 DOM 之后,为了描述元素的嵌套关系、属性、内容等,代码量骤增,并且可读性非常差。这就是命令式编程,我们需要一步一步地指挥计算机去做事
这还只是一个简单的静态页面,没有任何交互,试想一下,如果一个非常复杂的网页都是用 DOM 来构建,不好意思,我不想努力了~
观察上述 index.js,我们不难发现,在创建每个节点的时候其实可以抽象出一组重复操作:
对于元素的嵌套关系和自身属性,我们可以利用对象来描述
const appElement = {
type: "div",
props: {
className: "father",
children: [
{
type: "TEXT",
props: {
nodeValue: "Fucking React",
children: [],
},
},
{
type: "div",
props: {
className: "child",
children: [
{
type: "TEXT",
props: {
nodeValue: "用于构建用户界面的 JavaScript 库",
children: [],
},
},
],
},
},
],
},
};
其中,type表示元素类型,特殊地,对于字符串文本,我们用TEXT表示;props对象用来描述元素自身的属性,比如 CSS 类名、children 子元素、nodeValue
我们将页面中的元素用 JS 对象来描述,天然地形成了一种树状结构,接着利用递归遍历对象就可以将重复的 DOM 操作去除,我们构建如下 render 函数来将上述 JS 对象渲染到页面上:
const render = (element, container) => {
const dom =
element.type == "TEXT"
? document.createTextNode("")
: document.createElement(element.type);
Object.keys(element.props)
.filter((key) => key !== "children")
.forEach((prop) => (dom[prop] = element.props[prop]));
element.props.children.forEach((child) => render(child, dom));
container.appendChild(dom);
};
调用 render 函数:
render(appElement, document.getElementById("root"));
现在我们只需要将我们想要的页面结构通过 JS 对象描述出来,然后调用 render 函数,JS 就会帮我们将页面渲染出来,而无需一步步地书写每一步操作
这就是声明式编程,我们需要做的是描述目标的性质,让计算机明白目标,而非流程。
对比命令式和声明式编程,体会两者的区别
对比 JS 对象和 HTML,JS 对象的可读性还是不行,所以 React 引入了 JSX 这种 JavaScript 的语法扩展
我们的 appElement 变成了这样:
// jsx
const appElement = (
<div className="father">
Fucking React
<div className="child">"用于构建用户界面的 JavaScript 库"</div>
</div>
);
现在描述元素是不是变得超级爽!
然而这玩意儿 JS 并不认识,所以我们还得把这玩意儿解析成 JS 能认识的语法,解析不是本文的重点,所以我们借助于 babel 来进行转换,我们在浏览器中引入 babel
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
并将包含jsx的script的type改为type/babel
<script type="text/babel">
const appElement = (
<div className="father">
Fucking React
<div className="child">"用于构建用户界面的 JavaScript 库"</div>
</div>
);
</script>
默认情况下,babel 解析 jsx 时会调用React.createElement来创建 React 元素
我们可以自定义创建元素的方法,我们这里的元素就是我们自定义的对象,见 appElement。通过添加注解即可指定创建元素的方法,此处指定 createElement
const createElement = (type, props, ...children) => {
console.log(type);
console.log(props);
console.log(children);
};
/** @jsx createElement */
const appElement = (
<div className="father">
Fucking React
<div className="child">"用于构建用户界面的 JavaScript 库"</div>
</div>
);
现在 babel 进行转换的时候会调用我们自定义的 createElement 函数,该函数接受的参数分别为:元素类型type、元素属性对象props、以及剩余参数children即元素的子元素
现在我们要做的是通过这几个参数来创建我们需要的 js 对象,然后返回即可
const createElement = (type, props, ...children) => {
return {
type,
props: {
...props,
children,
},
};
};
/** @jsx createElement */
const appElement = (
<div className="father">
Fucking React
<div className="child">用于构建用户界面的 JavaScript 库</div>
</div>
);
console.log(appElement);
打印一下转换后的 appElement:
{
type: "div",
props: {
className: "father",
children: [
"Fucking React",
{
type: "div",
props: {
className: "child",
children: ["用于构建用户界面的 JavaScript 库"],
},
},
],
},
};
对比一下我们需要的结构,稍微有点问题,如果节点是字符串,我们需要转换成这种结构:
{
type: "TEXT",
props: {
nodeValue: "Fucking React",
children: [],
},
},
改进一下createElement
const createElement = (type, props, ...children) => {
return {
type,
props: {
...props,
children: children.map((child) =>
typeof child === "string"
? {
type: "TEXT",
props: {
nodeValue: child,
children: [],
},
}
: child
),
},
};
};
现在我们可以在代码中使用 jsx 而不用再写对象了,babel 会帮我们把 jsx 转换成对应的对象结构,然后调用 render 方法即可渲染到页面上
至此,我们完成了从命令式编程到声明式编程的转变,我们已经完成了“乞丐版 React”的功能有:
createElement创建元素render渲染元素到页面jsx接下来我们会从不同方向继续完善我们的“洪七公”,敬请期待!
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Build my react</title>
<style>
div {
text-align: center;
}
.father {
display: flex;
flex-direction: column;
justify-content: center;
height: 500px;
background-color: #282c34;
font-size: 30px;
font-weight: 700;
color: #61dafb;
}
.child {
color: #fff;
font-size: 16px;
font-weight: 200;
}
</style>
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
</head>
<body>
<div id="root"></div>
<script type="text/babel" src="./index.js"></script>
</body>
</html>
// index.js
const createElement = (type, props, ...children) => {
return {
type,
props: {
...props,
children: children.map((child) =>
typeof child === "string"
? {
type: "TEXT",
props: {
nodeValue: child,
children: [],
},
}
: child
),
},
};
};
/** @jsx createElement */
const appElement = (
<div className="father">
Fucking React
<div className="child">用于构建用户界面的 JavaScript 库</div>
</div>
);
const render = (element, container) => {
const dom =
element.type == "TEXT"
? document.createTextNode("")
: document.createElement(element.type);
Object.keys(element.props)
.filter((key) => key !== "children")
.forEach((prop) => (dom[prop] = element.props[prop]));
element.props.children.forEach((child) => render(child, dom));
container.appendChild(dom);
};
render(appElement, document.getElementById("root"));
我想用ruby编写一个小的命令行实用程序并将其作为gem分发。我知道安装后,Guard、Sass和Thor等某些gem可以从命令行自行运行。为了让gem像二进制文件一样可用,我需要在我的gemspec中指定什么。 最佳答案 Gem::Specification.newdo|s|...s.executable='name_of_executable'...endhttp://docs.rubygems.org/read/chapter/20 关于ruby-在Ruby中编写命令行实用程序
我正在使用active_admin,我在Rails3应用程序的应用程序中有一个目录管理,其中包含模型和页面的声明。时不时地我也有一个类,当那个类有一个常量时,就像这样:classFooBAR="bar"end然后,我在每个必须在我的Rails应用程序中重新加载一些代码的请求中收到此警告:/Users/pupeno/helloworld/app/admin/billing.rb:12:warning:alreadyinitializedconstantBAR知道发生了什么以及如何避免这些警告吗? 最佳答案 在纯Ruby中:classA
这篇文章是继上一篇文章“Observability:从零开始创建Java微服务并监控它(一)”的续篇。在上一篇文章中,我们讲述了如何创建一个Javaweb应用,并使用Filebeat来收集应用所生成的日志。在今天的文章中,我来详述如何收集应用的指标,使用APM来监控应用并监督web服务的在线情况。源码可以在地址 https://github.com/liu-xiao-guo/java_observability 进行下载。摄入指标指标被视为可以随时更改的时间点值。当前请求的数量可以改变任何毫秒。你可能有1000个请求的峰值,然后一切都回到一个请求。这也意味着这些指标可能不准确,你还想提取最小/
我从Ubuntu服务器上的RVM转移到rbenv。当我使用RVM时,使用bundle没有问题。转移到rbenv后,我在Jenkins的执行shell中收到“找不到命令”错误。我内爆并删除了RVM,并从~/.bashrc'中删除了所有与RVM相关的行。使用后我仍然收到此错误:rvmimploderm~/.rvm-rfrm~/.rvmrcgeminstallbundlerecho'exportPATH="$HOME/.rbenv/bin:$PATH"'>>~/.bashrcecho'eval"$(rbenvinit-)"'>>~/.bashrc.~/.bashrcrbenvversions
我有一个问题。我想从另一个ruby脚本运行一个ruby脚本并捕获它的输出信息,同时让它也输出到屏幕。亚军#!/usr/bin/envrubyprint"Enteryourpassword:"password=gets.chompputs"Hereisyourpassword:#{password}"我运行的脚本文件:开始.rboutput=`runner`putsoutput.match(/Hereisyour(password:.*)/).captures[0].to_s正如您在此处看到的那样,存在问题。在start.rb的第一行,屏幕是空的。我在运行程序中看不到“输入您的密
有这样的事吗?我想在Ruby程序中使用它。 最佳答案 试试这个http://csl.sublevel3.org/jp2a/此外,Imagemagick可能还有一些东西 关于ruby-是否有将图像文件转换为ASCII艺术的命令行程序或库?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/6510445/
如何在Ruby的if语句中检查bash命令的返回值(true/false)。我想要这样的东西,if("/usr/bin/fswscell>/dev/null2>&1")has_afs="true"elsehas_afs="false"end它会提示以下错误含义,它总是返回true。(irb):5:warning:stringliteralincondition正确的语法是什么?更新:/usr/bin/fswscell寻找afs安装和运行状态。它会抛出这样的字符串,Thisworkstationbelongstocell如果afs没有运行,命令以状态1退出 最
在几个项目中,我希望有一个类似rakeserver的rake任务,它将通过任何需要的方式开始为该应用程序提供服务。这是一个示例:task:serverdo%x{bundleexecrackup-p1234}end这行得通,但是当我准备停止它时,按Ctrl+c并没有正常关闭;它中断了Rake任务本身,它说rakeaborted!并给出堆栈跟踪。在某些情况下,我必须执行Ctrl+c两次。我可能可以用Signal.trap写一些东西来更优雅地中断它。有没有更简单的方法? 最佳答案 trap('SIGINT'){puts"Yourmessa
关于SSHkit-Github它说:Allbackendssupporttheexecute(*args),test(*args)&capture(*args)来自SSHkit-Rubydoc,我明白execute实际上是test的别名?test之间有什么区别?,execute,capture在Capistrano/SSHKit中我应该什么时候使用? 最佳答案 执行只是执行命令。使用非0退出引发错误。测试方法的行为与execute完全相同,但是它返回bool值(true如果命令以0退出,而false否则)。它通常用于控制任务中的流程
我在目录“C:\DocumentsandSettings\test.exe”中有一个文件,但是当我用单引号编写命令时`C:\DocumentsandSettings\test.exe(我无法在此框中显示),用于在Ruby中执行命令,我无法这样做,我收到的错误是找不到文件或目录。我尝试用“//”和“\”替换“\”,但似乎没有任何效果。我也使用过系统、IO.popen和exec命令,但所有的努力都是徒劳的。exec命令还使程序退出,这是我不想发生的。提前致谢。 最佳答案 反引号环境就像双引号,所以反斜杠用于转义。此外,Ruby会将空格解