react基础

react中文教程

React环境搭建

antd使用

https://ant-design.antgroup.com/components/overview-cn

  • 全局安装 npm install antd-init -g
  • antd-init
  • npm install antd –save

React

在你的组件显示在屏幕上之前,它们必须由 React 进行渲染。理解这个过程中的步骤有助于你思考你的代码如何执行并解释其行为。

想象一下,你的组件是厨房里的厨师,用食材制作出美味的菜肴。在这个场景中,React 是服务员,负责提出顾客的要求,并给顾客上菜。这个请求和服务 UI 的过程有三个步骤:

  • 触发渲染(将食客的订单送到厨房)
  • 渲染组件(在厨房准备订单)
  • 提交到 DOM(将订单送到桌前)

作为快照的状态

  • 与普通 JavaScript 变量不同,React 状态的行为更像一个快照。设置它并不改变你已有的状态变量,而是触发一次重新渲染。这在一开始可能会让人感到惊讶!
1
2
3
4
5
6
7
console.log(score);  // 0
setScore(score + 1); // setScore(0 + 1);
console.log(score); // 0
setScore(score + 1); // setScore(0 + 1);
console.log(score); // 0
setScore(score + 1); // setScore(0 + 1);
console.log(score); // 0

更新数组

避免使用 (会改变原始数组) 推荐使用 (会返回一个新数组)
添加元素 push,unshift concat,[…arr] 展开语法(例子)
删除元素 pop,shift,splice filter,slice(例子)
替换元素 splice,arr[i] = … 赋值 map(例子)
排序 reverse,sort 先将数组复制一份(例子)

Context使用场景

ref 和 state 的不同之处

ref state
useRef(initialValue)返回 { current: initialValue } useState(initialValue) 返回 state 变量的当前值和一个 state 设置函数 ( [value, setValue])
更改时不会触发重新渲染 更改时触发重新渲染。
可变 —— 你可以在渲染过程之外修改和更新 current 的值。 “不可变” —— 你必须使用 state 设置函数来修改 state 变量,从而排队重新渲染。
你不应在渲染期间读取(或写入) current 值。 你可以随时读取 state。但是,每次渲染都有自己不变的 state 快照。

Effect

1
2
3
4
5
const [count, setCount] = useState(0);
useEffect(() => {
setCount(count + 1);
});

爱因斯坦与佛陀

转载自: https://www.colombotelegraph.com/index.php/einstein-and-buddha-convergence-between-science-and-eastern-philosophy/

“Buddhism has the characteristics of what would be expected in a cosmic religion for the future: It transcends a personal God avoids dogmas and theology; it covers both the natural and spiritual; and it is based on a religious sense aspiring from the experience of all things, natural and spiritual, as a meaningful unity.

“If there is any religion that would cope with modern scientific needs it would be Buddhism. . (Albert Einstein)

Albert Einstein is possibly the greatest scientist mankind has ever produced. His general theory of relatively created a revolutionary change on how scientists have viewed the world. He discovered that time and space is always related to the observer. His famous equation E=MC2 revealed that matter and energy are interchangeable forms of same substance. Einstein being a genius did not confine his interest only on science. The views he has expressed on Religion, philosophy and politics indicates that he was a great thinker who tried to bridge the gap between science and philosophy or religion. Buddha gave us a great teaching which would lead to tap the maximum potential of the mind which will eventually lead to the understanding of everything happening around us and finally to liberate from the cycle of Sansara (Cycle of Birth and death). The difference between Einstein and Buddha is that while former was keen in finding answers to the phenomenon of outside world, Buddha used his own powers of observation within his mind (introspection), intellect and reasoning, grounded in reality, to guide him to his enlightenment. Both Buddha and Einstein did their research on a scientific basis. Buddha advised his followers NOT to accept what he was teaching them at face value or to take his beliefs “on faith.” Rather, he counseled them to test his theories for themselves, and if they didn’t prove true, then reject them. Buddha found what he was looking for. Einstein after all his discoveries has to admit mankind does not have the wisdom to understand the all the mysteries of the nature. The purpose of this article is to examine the relevancy of some of Einstein’s statement to Buddhist teachings and also to present Einstein’s view about the religion.

Einstein upheld the need for morality and rightness of the mankind. But he believed that morality should not come from fear or punishment expected from ‘God” or any other force. He said – A man’s ethical behavior should be based effectually on sympathy, education, and social ties and needs; no religious basis is necessary. Man would indeed be in a poor way if he had to be restrained by fear of punishment and hope of reward after death. If people are good only because they fear punishment, and hope for reward, then we are a sorry lot indeed- (Albert Einstein, “Religion and Science”, New York Times Magazine, 9 November 1930

Einstein denied the existence of a personal God but he could not provide an answer behind the beauty and methodical way universe has been formed. This he expressed in following way.

I do not believe in a personal God and I have never denied this but have expressed it clearly. If something is in me which can be called religious then it is the unbounded admiration for the structure of the world so far as our science can reveal it. I believe in Spinoza’s God who reveals himself in the orderly harmony of what exists, not in a God who concerns himself with the fates and actions of human beings. (Albert Einstein, 1954)

Who is this Spinoza’s God Einstein is refereeing to? Spinoza was a Dutch philosopher, who claimed that God is nothing but the NATURE According to Spinoza there is mass, energy, atoms, molecules, life, thought, people, societies, galaxies and perhaps even multiple universes but there is nothing outside nature, including spiritual visions and other phenomena we don’t yet understand. If they exist, they are part of nature. According to Buddhism there are five natural laws at work in the cosmos that cause things to happen, called the Five Niyamas. . Karma is only one of these factors. Present circumstances are the result of countless factors that are always in flux. There is no single cause that makes everything to be the way it is

The following statement clearly indicates that although Einstein rejected God he is not an atheist.

I have repeatedly said that in my opinion the idea of a personal God is a childlike one, but I do not share the crusading spirit of the professional atheist whose fervor is mostly due to a painful act of liberation from the fetters of religious indoctrination received in youth. I prefer an attitude of humility corresponding to the weakness of our intellectual understanding of nature and of our own being.

Buddha rejected the traditional Hindu view that world was a creation by God and also rejected materialism of atheists. He became the first philosopher to reject the belief ‘Soul’ is a permanent entity. It appears that Einstein accepted both these positions. In the above statement, Einstein was humble enough to admit the inadequacy or limitations of his knowledge to find the answer to the ‘mysteries’ of the world.

Just examine following statement of Einstein

“A human being is part of the whole, called by us ‘Universe’; a part limited in time and space. He experiences himself, his thoughts and feelings as something separated from the rest – a kind of optical delusion of his consciousness. This delusion is a kind of prison for us, restricting us to our personal desires and affection for a few persons nearest us. Our task must be to free ourselves from this prison by widening our circle of compassion to embrace all living creatures and the whole nature in its beauty. Nobody is able to achieve this completely but striving for such achievement is, in itself, a part of the liberation and a foundation for inner security.”

Physical concepts are free creations of the human mind, and are not, however it may seem, uniquely determined by the external world. -Einstein

It is amazing to know that Buddha, 2500 years back, expressed almost the same view in different words “All such notions as causation, succession, atoms, primary elements…are all figments of the imagination and manifestations of the mind. – -Buddha

According to Buddha, Self is not a rational concept. Self is only an emotional feeling- Therefore consciousness is an illusion and the feeling of ‘Self” is only an activity of brain. People live unconsciously in dream like state believing their existence. As a result they become attached to names and forms, not realising that they have no more basis than the activities of the mind itself. Buddha declared that due to this error in view, liberation of the mankind would be blocked. Buddha called it ignorance what Einstein described it as a delusion of consciousness. Buddha’s solution to free us from prison is by developing and purification of mind, following a clear path towards it (8 FP and 4NT). Buddha’s path also involves widening selfless compassion to all human beings. According to Buddha the attachment (Lust) brings suffering and would also prevent one from escaping the prison. Einstein says that this prison restricts our affection only to our loved ones (Becoming Selfish). He suggests that to free from the prison the compassion should embrace all living creatures. (It is interesting Einstein did not confine compassion only to human beings)

Einstein’s attempt to discover the mystery of the world can be seen from this statement.

The most beautiful and most profound experience is the sensation of the mystical. It is the sower of all true science. He to whom this emotion is a stranger, who can no longer wonder and stand rapt in awe, is as good as dead. To know that what is impenetrable to us really exists, manifesting itself as the highest wisdom and the most radiant beauty which our dull faculties can comprehend only in their primitive forms – this knowledge, this feeling is at the center of true religiousness. (Albert Einstein – The Merging of Spirit and Science)

Here again Einstein admits that human mind cannot comprehend all the mysteries of the world. But he admired the beauty and methodical way nature operates it. He admitted the answer comes with more wisdom and he also termed it as true religiousness. Buddha’s approach for what Einstein has been looking could be evident from following interesting Dialogue between Buddha and one of his disciples.

“Most respected one; can you please tell me what the size of the universe is?”

Buddha replied:

“Will the size of the universe help you end suffering?”

The disciple answered:

“No, most respected one”

Buddha then answered:

“So why are you asking questions of little importance and not ask questions on the ending of suffering?

Buddhism avoids speculative metaphysical questions as irrelevant distractions. According to Buddhism, nothing happens without a cause; therefore the universe is infinite with no real beginning. However, it does run in cycles, so there can be a beginning for each particular era.

This Parable of the arrow has often been used to illustrate the Buddha’s teachings that “practitioners who concern themselves with the origins of the universe and other topics are missing the point of religious practice.”

Suppose someone was hit by a poisoned arrow and his friends and relatives found a doctor able to remove the arrow. If this man were to say, ‘I will not have this arrow taken out until I know whether the person who had shot it was a priest, a prince or a merchant, his name and his family. I will not have it taken out until I know what kind of bow was used and whether the arrowhead was an ordinary one or an iron one.’ That person would die before all these things are ever known to

In short, work on being here (The Present moment) before you consider why or how you came to be here.

Once Bertrand Russell explained why he accepts the Buddhist view on origin of the world. -” Among the founders of all religions in this world, I respect only one man — the Buddha. The main reason was that the Buddha did not make statements regarding the origin of the world. The Buddha was the only teacher who realised the true nature of the world.” (Bertrand Russell)

It is interesting to note that Modern scientists and psychologists pay great attention to research on brain and mind. Buddhist mediation methods of loving kindness, breathing mediation and mindfulness are widely used in western medicine and psychology. It would be great if more scientists like Einstein come forward to bridge the gap between the science and eastern philosophy in future.

epoll的水平触发和边缘触发模式

参考: https://cloud.tencent.com/developer/article/1636224

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/**
* 验证epoll的LT与ET模式的区别, epoll_server.cpp
* zhangyl 2019.04.01
*/
#include<sys/types.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<unistd.h>
#include<fcntl.h>
#include<sys/epoll.h>
#include<poll.h>
#include<iostream>
#include<string.h>
#include<vector>
#include<errno.h>
#include<iostream>


// 测试ET模式(边缘触发模式)
#define TEST_ET_MODE


int main()
{
//创建一个监听socket
int listenfd = socket(AF_INET, SOCK_STREAM, 0);
if (listenfd == -1)
{
std::cout << "create listen socket error" << std::endl;
return -1;
}

//设置重用ip地址和端口号
int on = 1;
setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, (char*)& on, sizeof(on));
setsockopt(listenfd, SOL_SOCKET, SO_REUSEPORT, (char*)& on, sizeof(on));


//将监听socker设置为非阻塞的
int oldSocketFlag = fcntl(listenfd, F_GETFL, 0);
int newSocketFlag = oldSocketFlag | O_NONBLOCK;
if (fcntl(listenfd, F_SETFL, newSocketFlag) == -1)
{
close(listenfd);
std::cout << "set listenfd to nonblock error" << std::endl;
return -1;
}

//初始化服务器地址
struct sockaddr_in bindaddr;
bindaddr.sin_family = AF_INET;
bindaddr.sin_addr.s_addr = htonl(INADDR_ANY);
bindaddr.sin_port = htons(3000);

if (bind(listenfd, (struct sockaddr*) & bindaddr, sizeof(bindaddr)) == -1)
{
std::cout << "bind listen socker error." << std::endl;
close(listenfd);
return -1;
}

//启动监听
if (listen(listenfd, SOMAXCONN) == -1)
{
std::cout << "listen error." << std::endl;
close(listenfd);
return -1;
}


//创建epollfd
int epollfd = epoll_create(1);
if (epollfd == -1)
{
std::cout << "create epollfd error." << std::endl;
close(listenfd);
return -1;
}

epoll_event listen_fd_event;
listen_fd_event.data.fd = listenfd;
listen_fd_event.events = EPOLLIN;
//取消注释掉这一行,则使用ET模式
#ifdef TEST_ET_MODE
listen_fd_event.events |= EPOLLET;
#endif

//将监听sokcet绑定到epollfd上去
if (epoll_ctl(epollfd, EPOLL_CTL_ADD, listenfd, &listen_fd_event) == -1)
{
std::cout << "epoll_ctl error" << std::endl;
close(listenfd);
return -1;
}

int n;
while (true)
{
epoll_event epoll_events[1024];
n = epoll_wait(epollfd, epoll_events, 1024, 1000);
if (n < 0)
{
//被信号中断
if (errno == EINTR)
continue;

//出错,退出
break;
}
else if (n == 0)
{
//超时,继续
continue;
}
for (size_t i = 0; i < n; ++i)
{
//事件可读
if (epoll_events[i].events & EPOLLIN)
{
if (epoll_events[i].data.fd == listenfd)
{
//侦听socket,接受新连接
struct sockaddr_in clientaddr;
socklen_t clientaddrlen = sizeof(clientaddr);
int clientfd = accept(listenfd, (struct sockaddr*) & clientaddr, &clientaddrlen);
if (clientfd != -1)
{
int oldSocketFlag = fcntl(clientfd, F_GETFL, 0);
int newSocketFlag = oldSocketFlag | O_NONBLOCK;
if (fcntl(clientfd, F_SETFD, newSocketFlag) == -1)
{
close(clientfd);
std::cout << "set clientfd to nonblocking error." << std::endl;
}
else
{
epoll_event client_fd_event;
client_fd_event.data.fd = clientfd;
//同时侦听新来连接socket的读和写时间
client_fd_event.events = EPOLLIN | EPOLLOUT;
//取消注释这一行,则使用ET模式
client_fd_event.events |= EPOLLET;
if (epoll_ctl(epollfd, EPOLL_CTL_ADD, clientfd, &client_fd_event) != -1)
{
std::cout << "new client accepted,clientfd: " << clientfd << std::endl;
}
else
{
std::cout << "add client fd to epollfd error" << std::endl;
close(clientfd);
}
}
}
}
else
{
std::cout << "client fd: " << epoll_events[i].data.fd << " recv data." << std::endl;
//普通clientfd
char recvbuf[1024] = { 0 };

#ifdef TEST_ET_MODE
// 边缘出发,必须一次性收完
int m = 0;
do
{
m = recv(epoll_events[i].data.fd, recvbuf, 1, 0 );
if(m > 0)
{
std::cout << "recv from client:" << epoll_events[i].data.fd << ", " << recvbuf << std::endl;
}
bzero(recvbuf, sizeof(recvbuf));
}while(m > 0);
#else
// 水平触发,每次只收一个字节, 不影响
int m = recv(epoll_events[i].data.fd, recvbuf, 1, 0);
#endif
if (m == 0)
{
//对端关闭了连接,从epollfd上移除clientfd
if (epoll_ctl(epollfd, EPOLL_CTL_DEL, epoll_events[i].data.fd, NULL) != -1)
{
std::cout << "client disconnected,clientfd:" << epoll_events[i].data.fd << std::endl;
}
close(epoll_events[i].data.fd);
}
else if (m < 0)
{
//出错
if (errno != EWOULDBLOCK && errno != EINTR)
{
if (epoll_ctl(epollfd, EPOLL_CTL_DEL, epoll_events[i].data.fd, NULL) != -1)
{
std::cout << "client disconnected,clientfd:" << epoll_events[i].data.fd << std::endl;
}
close(epoll_events[i].data.fd);
}
}
else
{
//正常收到数据
std::cout << "recv from client:" << epoll_events[i].data.fd << ", " << recvbuf << std::endl;

epoll_event client_fd_event;
client_fd_event.data.fd = epoll_events[i].data.fd;

#ifndef TEST_ET_MODE
// 水平触发模式下,需要再次给clientfd注册检测可写事件, 来接受socket上次没接收的数据
client_fd_event.events = EPOLLIN | EPOLLOUT ;
// client_fd_event.events |= EPOLLET;
if (epoll_ctl(epollfd, EPOLL_CTL_MOD, epoll_events[i].data.fd, &client_fd_event) != -1)
{
std::cout << "epoll_ctl successfully, mode: EPOLL_CTL_MOD, clientfd:" << epoll_events[i].data.fd << std::endl;
}
#endif

}
}
}
else if (epoll_events[i].events & EPOLLOUT)
{
//只处理客户端fd的可写事件
if (epoll_events[i].data.fd != listenfd)
{
// 水平触发模式下,会一直触发,打印结果
std::cout << "EPOLLOUT triggered,clientfd:" << epoll_events[i].data.fd << std::endl;
}
}
else if (epoll_events[i].events & EPOLLERR)
{
// TODO 暂不处理
}
}
}

close(listenfd);
return 0;
}

佛教中“空”的本义是什么

一切万有都是缘起,这就是佛教“空”之本义,“无”之真意。

空,是佛教对世界一切事物本质最简要的描述,是释迦世尊洞彻世界万物缘起的结论。故《心经》开头就说:“观自在菩萨,行深般若波罗蜜多时,照见五蕴皆空,度一切苦厄。舍利子,色不异空,空不异色,色即是空,空即是色,受想行识,亦复如此。”

但社会上很多人都误解了佛教的“空”字。有人反问说:“我们每天吃的、穿的、用的,以及看到的一切,难道都是空的吗?”说这话的人也许没认真思维,难道佛教徒糊涂到连日常生活都搞不清楚吗?!佛教所说的“空”,根本不是有人所认为的“没有”的意思,也与社会上常听到的“人生到头一场空”的“空”字意思有别,“一场空”的“空”字,是得不到的意思。

佛教所说的“空”,是对世间事物变化过程的描述,准确地讲,是万事万物最终要变化、要毁坏的意思,是对缘起法最本质的体证。佛教认为世上任何一种事物,都是由因缘和合而成,都是由各种条件和合而成,或其它事物变化而来,最后又变化为另外的事物

成、住、坏、空,是世上一切事物发展变化的必然,有生就有死,有成就有坏。佛教所说的空,是指事物的本性是空的,而不是指事物的外相也是空。比如我们的一辈子,父母亲生下我们时,佛法上说是四大五蕴和合而成,按科学说法,是碳、氢、氧等基本元素构成,然后长大成人,然后衰老,最后死亡,一切都是变化的,我们的一生也就是在这样的因缘变化中过去了。正是在这个意思上,我们说人生是空的,一切都是缘起性空。

所以佛法教导我们,做人不要执着于外相上的缘起缘灭,执着就苦,就起烦恼。但也不要因为认识到人生是空的,就悲观绝望,这就落入了顽空,这也是一种固执。因为“性空”并不否定相上的缘起,相是存在的,它刹那生灭变化。

一个人认识到人生的空苦,而又不落入顽空,确实需要智慧。如何获得这种智慧?这正是佛教所要解决、所能解决的。通过学佛,修戒定慧三学,体证般若的智慧,达到改变我们苦空人生的目的。

  • Copyrights © 2021-2024 youngqqcn

请我喝杯咖啡吧~