2021年12月4日
react学习day1
React介绍:
由facebook推出的数据驱动JS库
基于MVC灵感设计(vue是基于mvvm)
特性:数据驱动 最小粒度更新(diff/fiber) 组件化
React项目创建:
1. 下载安装脚手架
yarn global add create-react-app
2. 创建项目
create-react-app 项目名(项目名及路径不能有中文)
3. 启动项目(react服务器端口默认3000)
cd 项目名
yarn start || npm run start
JSX模板语法糖:
允许在JS中混写HTML标记,每一段在JS中的HTML标记,它就是一个小的JSX
(
es字符串模板语法 <div>${xxx}</div>
template模板语法 {{ xxx }}
jsx模板语法 { xxx }
)
模板语法:可以在当前模板中,嵌入一些变量或者函数等动态数据..
JSX底层:
JSX是语法糖,所以写的每一个JSX标记(template中的节点),都会被转译为虚拟DOM节点
//当把JSX节点转译成虚拟DOM时,会把中间{}中的值替换为真正的值
<div>{ city }</div> === React.createElement('div', div的属性,'city真正的值')
JSX的特性:
1. JSX中采用的是{}进行占位,在{}中可以书写任意表达式
2. JSX不同于HTML,它的事件绑定采用的是驼峰
例: <button onClick={ 要触发的函数名 } ></button>
3. 动态属性绑定,采用{}进行占位!!!(react所见即所得,哪里要动哪里就加占位符)
例: <button id={xxxx} name={xxxx} />
4. class是js中的关键字,所以绑定class是采用className别名
5. style必须给一个对象,不支持字符串
例: <h1 style={ {color: 'xxx', fontWeight: 'xxx' } }>111</h1>
组件化开发:
React自定义组件创建:
1. 引入核心模块(React项目中,一个组件就是一个.js文件,
组件.js,组件文件首字母大写 )
import React from ‘react’
2. 组件创建并定义视图
class 组件名(首字母大写) extends React.Component {
render(){
return <div>
组件视图定义再此处 //类似template
</div>
}
}
3. 暴露
export default 组件名
React组件的使用:
1. 引入自定义组件
import 自定义组件名 from './组件文件路径'
2. 使用标签直接渲染
<自定义组件名/> //单双标签都支持
组件状态:
每个组件内部需要变化的动态数据,就叫组件状态
在react中,叫state (在vue中,叫data)
1. 状态定义
class 组件 extends xxx {
state = {
状态名: 状态值
}
render(){
...
}
}
2. 获取状态的值
JS中: this.state.状态名
JSX中: {this.state.状态名}
class 组件 extends xxx {
state = {
状态名: 状态值,
city: '贵阳市'
}
render(){
//进阶取值方式,解构
let { city } = this.state
return <div>
<p>我的家乡是: {this.state.city}</p>
<p>解构取值: {city}</p>
</div>
}
}
3. 改变状态的值
//注意在自定义函数中,this指向问题!!!!!!
REACT是基于MVC写出来的,所以没有监听数据变化!!!必须使用setState改变状态,通知视图自动更新
this.setState({
状态名: 新的值
})
//每个组件身上都有setState函数,任何组件状态改变都要用此函数!!!
//setState做了2件事:1. 改变state数据 2. 通知视图最小粒度更新
Hi there, every time i used to check webpage posts here in the
early hours in the dawn, since i love to learn more and more.
You actually make it appear so easy together with your presentation however I in finding this matter to be actually
one thing which I think I would by no means understand.
It seems too complex and extremely wide for me.
I’m taking a look forward for your subsequent put up, I’ll try to get the grasp of it!
I know this site offers quality depending articles or reviews and other material, is
there any other web site which presents these kinds of stuff in quality?
This information is worth everyone’s attention. When can I find out more?
I like the valuable information you provide in your articles.
I’ll bookmark your blog and check again here regularly. I am quite certain I will learn many new
stuff right here! Good luck for the next!
I got this web site from my buddy who informed me concerning this web page and
now this time I am browsing this website and reading very informative articles at this time.
Hello! This post couldn’t be written any better!
Reading this post reminds me of my previous room mate!
He always kept chatting about this. I will forward this page to him.
Fairly certain he will have a good read. Thanks for
sharing!
Thanks for sharing your thoughts about seo firm. Regards
I do not know if it’s just me or if everyone else experiencing problems with your blog.
It seems like some of the written text in your posts
are running off the screen. Can somebody else please comment and let me know if this is happening to
them as well? This might be a problem with my web
browser because I’ve had this happen before. Appreciate it
I know this website provides quality based articles or reviews and extra stuff, is
there any other site which provides these kinds of stuff in quality?
http://mistiktogel.cfd/
Magnificent goods from you, man. I have understand your stuff previous to and you’re just
too fantastic. I really like what you have acquired here, really
like what you’re stating and the way in which you say it. You make it entertaining
and you still care for to keep it smart. I can’t wait to
read far more from you. This is really a great
site.
Hmm is anyone else experiencing problems with the images on this blog loading?
I’m trying to figure out if its a problem on my end or if it’s
the blog. Any responses would be greatly appreciated.
I read this post completely on the topic of the difference of most up-to-date and previous technologies, it’s awesome article.
I was recommended this website by my cousin. I’m not sure whether this post is written by him as no one else
know such detailed about my difficulty. You’re wonderful!
Thanks!
A motivating discussion is definitely worth comment. I believe that you ought to write more about this
subject matter, it may not be a taboo subject but usually people do not discuss these topics.
To the next! Cheers!!
I’m gone to convey my little brother, that he should also pay a
visit this web site on regular basis to obtain updated from newest reports.
I really like reading a post that can make people think.
Also, many thanks for allowing me to comment!
If some one desires expert view on the topic of blogging then i advise him/her to pay a quick visit this blog, Keep up the
pleasant work.
What’s up to every body, it’s my first visit of this web site;
this blog consists of amazing and actually fine
information for readers.
It is really a nice and useful piece of information. I’m happy that you just shared this helpful information with us.
Please keep us informed like this. Thanks for sharing.
I don’t even understand how I ended up here,
however I assumed this publish was good. I do not recognize who you might be
however definitely you are going to a well-known blogger in case you
aren’t already. Cheers!
Here is my page try this out
I’m gone to say to my little brother, that he should
also go to see this website on regular basis to take updated from hottest news.
You should be a part of a contest for one of the most useful websites on the web.
I most certainly will recommend this website!
After checking out a handful of the blog articles on your web site, I seriously appreciate your way of blogging.
I saved it to my bookmark website list and will be checking
back soon. Take a look at my web site as well and let me
know how you feel.
Hey there! This is my first comment here so I just wanted
to give a quick shout out and say I really enjoy reading
your articles. Can you suggest any other blogs/websites/forums that go over the
same subjects? Many thanks!
Awesome! Its in fact remarkable post, I have got much clear
idea on the topic of from this post.
I am regular reader, how are you everybody? This paragraph posted
at this web site is actually fastidious.
Also visit my blog click here to investigate
I really like what you guys tend to be up too.
This sort of clever work and exposure! Keep up the wonderful works guys I’ve included you guys to
my blogroll.
It’s amazing to go to see this website and reading the views of all mates
on the topic of this article, while I am also zealous of getting familiarity.
Review my web-site … useful content
Way cool! Some very valid points! I appreciate
you writing this write-up plus the rest of the site is also very
good.
Here is my blog … look at this now
Great blog! Is your theme custom made or did you download it from somewhere?
A design like yours with a few simple adjustements would really make my blog shine.
Please let me know where you got your theme. Appreciate it
Also visit my page discover here
What’s up all, here every one is sharing such know-how,
thus it’s pleasant to read this web site, and I used to go to see this webpage
everyday.
Here is my homepage – look at here now
You’ve made some good points there. I looked on the net for more information about the
issue and found most individuals will go
along with your views on this site.
Good way of explaining, and fastidious article to take information about
my presentation subject, which i am going to present in school.
Wow that was odd. I just wrote an extremely long comment but after I clicked submit
my comment didn’t appear. Grrrr… well I’m not writing all that over again. Regardless,
just wanted to say wonderful blog!
What’s up, I want to subscribe for this blog to obtain most recent updates, thus where can i do it please assist.
Hi would you mind letting me know which webhost you’re working with?
I’ve loaded your blog in 3 different internet browsers and I must
say this blog loads a lot quicker then most. Can you recommend a good hosting provider at a fair price?
Thanks, I appreciate it!
I get pleasure from, result in I discovered exactly what I used to be looking for.
You’ve ended my 4 day long hunt! God Bless
you man. Have a great day. Bye
kchqi – Easy to find what I’m looking for, very user-friendly structure.
kchqi – This site gives a welcoming vibe, feels trustworthy right away.
Greetings! I know this is kinda off topic but I was wondering if you knew where I could
find a captcha plugin for my comment form?
I’m using the same blog platform as yours and I’m having trouble finding one?
Thanks a lot!