下面是一段JSX語法數組
<script type="text/babel">
const element = <h2>Hello World</h2>
ReactDOM.render(element, document.getElementById("app"));
</script>
複製代碼
JSX是什麼?bash
爲何React選擇了JSX?babel
React認爲渲染邏輯本質上與其餘UI邏輯存在內在耦合app
他們之間是密不可分,因此React沒有將標記分離到不一樣的文件中,而是將它們組合到了一塊兒,這個地方就是組件(Component),JSX實際上是嵌入到JavaScript中的一種結構語法;函數
JSX的書寫規範:學習
return (
<div>
<h2>電影列表1</h2>
<ul>
{liArray}
</ul>
<h2>電影列表2</h2>
<ul>
{
this.state.movies.map((item) => {
return <li>{item}</li>
})
}
</ul>
</div>
)
複製代碼
若是咱們jsx中的內容是動態的,咱們能夠經過表達式來獲取:ui
jsx是嵌入到JavaScript中的一種語法,因此在編寫註釋時,須要經過JSX的語法來編寫:this
<div>
{/* 我是一段註釋 */}
<h2>Hello World</h2>
</div>
複製代碼
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
name: "why",
age: 18,
hobbies: ["籃球", "唱跳", "rap"],
test1: null,
test2: undefined,
flag: false,
friend: {
name: "kobe",
age: 40
}
}
}
render() {
return (
<div>
{/* 我是一段註釋 */}
<h2>Hello World</h2>
</div>
<div>
{/* 1.能夠直接顯示 */}
<h2>{this.state.name}</h2>
<h2>{this.state.age}</h2>
<h2>{this.state.hobbies}</h2>
{/* 2.不顯示 */}
<h2>{this.state.test1}</h2>
<h2>{this.state.test1 + ""}</h2>
<h2>{this.state.test2}</h2>
<h2>{this.state.test2 + ""}</h2>
<h2>{this.state.flag}</h2>
<h2>{this.state.flag + ""}</h2>
{/* 3.報錯,不顯示 */}
<h2>123{this.state.friend}</h2>
</div>
)
}
}
ReactDOM.render(<App/>, document.getElementById("app"));
複製代碼
說明:爲何null、undefined、Boolean在JSX中要顯示爲空內容呢?spa
這個時候,咱們能夠編寫以下代碼:code
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
flag: false
}
}
render() {
return (
<div>
{this.state.flag ? <h2>我是標題</h2>: null}
{this.state.flag && <h2>我是標題</h2>}
</div>
)
}
}
複製代碼
JSX中,也能夠是一個表達式。
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
firstName: "kobe",
lastName: "bryant",
age: 20
}
}
render() {
return (
<div>
{/* 運算表達式 */}
<h2>{this.state.firstName + " " + this.state.lastName}</h2>
{/* 三元運算符 */}
<h2>{this.state.age >= 18 ? "成年人": "未成年人"}</h2>
{/* 執行一個函數 */}
<h2>{this.sayHello("kobe")}</h2>
</div>
)
}
sayHello(name) {
return "Hello " + name;
}
}
複製代碼
不少時候,描述的HTML原生會有一些屬性,而咱們但願這些屬性也是動態的:
下面爲各種屬性綁定寫法
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
title: "你好啊",
imgUrl: "https://upload.jianshu.io/users/upload_avatars/1102036/c3628b478f06.jpeg?imageMogr2/auto-orient/strip|imageView2/1/w/240/h/240",
link: "https://www.baidu.com",
active: false
}
}
render() {
return (
<div>
<h2 title={this.state.title}>Hello World</h2>
<img src={this.state.imgUrl} alt=""/>
<a href={this.state.link} target="_blank">百度一下</a>
<div className={"message " + (this.state.active ? "active": "")}>你好啊</div>
<div className={["message", (this.state.active ? "active": "")].join(" ")}>你好啊</div>
<div style={{fontSize: "30px", color: "red", backgroundColor: "blue"}}>我是文本</div>
</div>
)
}
}
複製代碼
原生DOM原生有一個監聽事件,常規操做通常爲:
<button onclick="btnClick()">點我一下</button> //btnClick()這樣寫的緣由是onclick綁定的後面是跟上JavaScript代碼;
<script>
function btnClick() {
console.log("按鈕發生了點擊");
}
</script>
複製代碼
在React中是如何操做de?
React中的事件監聽,這裏主要有兩點不一樣
class App extends React.Component {
render() {
return (
<div>
<button onClick={this.btnClick}>點我一下(React)</button>
</div>
)
}
btnClick() {
console.log("React按鈕點擊了一下")
}
}
複製代碼
在事件執行後,咱們可能須要獲取當前類的對象中相關的屬性:
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
message: "你好啊,李銀河"
}
}
render() {
return (
<div>
<button onClick={this.btnClick}>點我一下(React)</button>
</div>
)
}
btnClick() {
console.log(this);
console.log(this.state.message);
}
}
複製代碼
爲何是undefined呢?
如何解決this的問題呢? 方案一:bind給btnClick顯示綁定this
<button onClick={this.btnClick.bind(this)}>點我一下(React)</button>
複製代碼
但若是我有兩個函數都須要用到btnClick的綁定咱們發現 bind(this) 須要書寫兩遍;:
<button onClick={this.btnClick.bind(this)}>點我一下(React)</button>
<button onClick={this.btnClick.bind(this)}>也點我一下(React)</button>
複製代碼
咱們能夠經過在構造方法中直接給this.btnClick綁定this來解決(注意查看 constructor 中咱們的操做:this.btnClick = this.btnClick.bind(this);):
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
message: "你好啊,李銀河"
}
this.btnClick = this.btnClick.bind(this);
}
render() {
return (
<div>
<button onClick={this.btnClick}>點我一下(React)</button>
<button onClick={this.btnClick}>也點我一下(React)</button>
</div>
)
}
btnClick() {
console.log(this);
console.log(this.state.message);
}
}
複製代碼
方案二:使用 ES6 class fields 語法
你會發現我這裏將btnClick的定義變成了一種賦值語句:
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
message: "你好啊,李銀河"
}
}
render() {
return (
<div>
<button onClick={this.btnClick}>點我一下(React)</button>
<button onClick={this.btnClick}>也點我一下(React)</button>
</div>
)
}
btnClick = () => {
console.log(this);
console.log(this.state.message);
}
}
複製代碼
方案三:事件監聽時傳入箭頭函數(推薦)
由於 onClick 中要求咱們傳入一個函數,那麼咱們能夠直接定義一個箭頭函數傳入:
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
message: "你好啊,李銀河"
}
}
render() {
return (
<div>
<button onClick={() => this.btnClick()}>點我一下(React)</button>
<button onClick={() => this.btnClick()}>也點我一下(React)</button>
</div>
)
}
btnClick() {
console.log(this);
console.log(this.state.message);
}
}
複製代碼
在執行事件函數時,有可能咱們須要獲取一些參數信息:好比event對象、其餘參數
狀況一:獲取event對象
class App extends React.Component {
constructor(props) {
render() {
return (
<div>
<a href="http://www.baidu.com" onClick={this.btnClick}>點我一下</a>
</div>
)
}
btnClick(e) {
e.preventDefault();
console.log(e);
}
}
複製代碼
狀況二:獲取更多參數
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
names: ["衣服", "鞋子", "褲子"]
}
}
render() {
return (
<div>
<a href="http://www.baidu.com" onClick={this.aClick}>點我一下</a>
{
this.state.names.map((item, index) => {
return (
<a href="#" onClick={e => this.aClick(e, item, index)}>{item}</a>
)
})
}
</div>
)
}
aClick(e, item, index) {
e.preventDefault();
console.log(item, index);
console.log(e);
}
}
複製代碼
本文是經過學習codewhy的 React課程總結