背景: 3月份離職了宛瞄,之后入職新公司胆胰,第一個(gè)任務(wù)是react + ts + node項(xiàng)目赞枕;由于沒有react開發(fā)經(jīng)驗(yàn)就先過了一遍文檔味悄,在開始敲代碼的時(shí)候總是會(huì)忘記語法草戈,然后就做了個(gè)react 基礎(chǔ)語法和注意事項(xiàng) 相關(guān)的文檔,加一些和vue使用的區(qū)別侍瑟,有react經(jīng)驗(yàn)的小伙伴就不需要浪費(fèi)時(shí)間往下看啦~~
那么就開始吧~
- 變量作為屬性值及模版中嵌入表達(dá)式
<!--react-->
<span type={user.type}>{user.name}</span>
<!--vue-->
<span :type="user.type">{{user.name}}</span>
單向數(shù)據(jù)流
react 通過setState
來修改唐片,方便定位問題;
所有 React 組件都必須像純函數(shù)一樣保護(hù)它們的 props 不被更改
vue也是單向數(shù)據(jù)流不過也還是可以通過黑科技改變props的模版嵌套
組件的嵌套設(shè)計(jì),復(fù)雜大型功能自下而上涨颜,小型功能自上而下费韭,方便處理和測試react 的 state
組件私有屬性,用與子組件共享庭瑰,用于可變狀態(tài)(mutable state)星持;
react如果子組件共用且均需要修改state時(shí)要將state提升到公共父級去,修改需使用setState來處理最小state
通過父級props傳進(jìn)來弹灭;隨著時(shí)間的推移保持不變督暂;能根據(jù)其他props和state技術(shù)出來;
以上3種情況都不應(yīng)該另用新state-
組件不使用state時(shí)可寫成函數(shù)形式
function LogoutButton(props) { return ( <button onClick={props.onClick}> Logout </button> ); } <!--組件使用--> <LogoutButton />
反向數(shù)據(jù)流
子組件通知父組件修改state穷吮,通過調(diào)用回調(diào)函數(shù)來解決逻翁,將父組件函數(shù)傳入子組件來進(jìn)行處理;不同于vue是子組件emit事件,父級接收到emit來執(zhí)行相關(guān)函數(shù)-
阻止默認(rèn)事件
不能通過返回false
的方式阻止默認(rèn)行為捡鱼。你必須顯式的使用preventDefault
<!--無法阻止--> <a href="#" onclick="console.log('The link was clicked.'); return false"> Click me </a>
<!--正確做法--> function ActionLink() { function handleClick(e) { e.preventDefault(); console.log('The link was clicked.'); } return ( <a href="#" onClick={handleClick}> Click me </a> ); }
組件名開頭必須大寫
React 會(huì)將以小寫字母開頭的組件視為原生 DOM 標(biāo)簽八回。 <div> 代表 HTML 的 div 標(biāo)簽,而 <Welcome />則代表一個(gè)組件,并且需在作用域內(nèi)使用 Welcome缠诅。規(guī)范詳見深入JSXstyle
在模板上直接使用要用‘雙括號(hào)’
定義style對象添加
<!--直接在模版添加-->
<div
className="board-row"
style={{color: 'red'}}
>喂喂溶浴,testtest</div>
<!--style變量-->
const devClass = {color:red};
<div style={ devClass, ..., ....}></div>
<!--如果是font-size等有中線的要改為駝峰-->
<div style={{fontSize:'14px'}} key={item.title}>
-
class
在react中class是保留字因此以className
替換,其他差異詳見react屬性差異function Square(props) { return ( <button className="square" onClick={props.onClick}> {props.value} </button> ); }
-
對比于 vue slot
- react 子組件通過
props.children
可以實(shí)現(xiàn)管引;比起slot props
可以傳遞任何東西
function FancyBorder(props) { return ( <div> <!--如果父級傳入children將會(huì)被覆蓋--> {props.children} </div> ); } function WelcomeDialog() { return ( <!--此處children不會(huì)被接收士败,會(huì)被子組件所覆蓋--> <FancyBorder color="blue" children={11}> <h1 className="Dialog-title"> Welcome </h1> </FancyBorder> ); } ReactDOM.render( <WelcomeDialog />, document.getElementById('root') );
- 父組件直接將組件傳入及子組件
props[any]
獲取
- react 子組件通過
-
fragment
render return時(shí)需要包裹dom元素;
React 中的一個(gè)常見模式是一個(gè)組件返回多個(gè)元素。Fragments 允許你將子列表分組汉匙,而無需向 DOM 添加額外節(jié)點(diǎn)render() { return ( <React.Fragment> <ChildA /> <ChildB /> <ChildC /> </React.Fragment> ); }
你可以使用一種新的拱烁,且更簡短的語法來聲明 Fragments;可以像使用任何其他元素一樣使用
<>
</>
,除了它不支持 key 或?qū)傩?/strong>class Columns extends React.Component { render() { return ( <> <td>Hello</td> <td>World</td> </> ); } }
-
參數(shù)傳遞噩翠,詳見Context
- context:設(shè)計(jì)目的是為了共享那些對于一個(gè)組件樹而言是“全局”的數(shù)據(jù);主要應(yīng)用場景在于很多不同層級的組件需要訪問同樣一些的數(shù)據(jù);
// Context 可以讓我們無須明確地傳遍每一個(gè)組件戏自,就能將值深入傳遞進(jìn)組件樹。 // 為當(dāng)前的 theme 創(chuàng)建一個(gè) context(“l(fā)ight”為默認(rèn)值)伤锚。 const ThemeContext = React.createContext('light'); class App extends React.Component { render() { // 使用一個(gè) Provider 來將當(dāng)前的 theme 傳遞給以下的組件樹擅笔。 // 無論多深,任何組件都能讀取這個(gè)值屯援。 // 在這個(gè)例子中猛们,我們將 “dark” 作為當(dāng)前的值傳遞下去。 return ( <ThemeContext.Provider value="dark"> <Toolbar /> </ThemeContext.Provider> ); } } // 中間的組件再也不必指明往下傳遞 theme 了狞洋。 function Toolbar(props) { return ( <div> <ThemedButton /> </div> ); } class ThemedButton extends React.Component { // 指定 contextType 讀取當(dāng)前的 theme context弯淘。 // React 會(huì)往上找到最近的 theme Provider,然后使用它的值吉懊。 // 在這個(gè)例子中庐橙,當(dāng)前的 theme 值為 “dark”。 static contextType = ThemeContext; render() { return <Button theme={this.context} />; } }
- 組件組合(將組件傳入)
<!--原--> <Page user={user} avatarSize={avatarSize} /> // ... 渲染出 ... <PageLayout user={user} avatarSize={avatarSize} /> // ... 渲染出 ... <NavigationBar user={user} avatarSize={avatarSize} /> // ... 渲染出 ... <Link href={user.permalink}> <Avatar user={user} size={avatarSize} /> </Link> <!--改:只有最頂部的 Page 組件需要知道 Link 和 Avatar 組件是如何使用 user 和 avatarSize 的借嗽。--> function Page(props) { const user = props.user; const userLink = ( <Link href={user.permalink}> <Avatar user={user} size={props.avatarSize} /> </Link> ); return <PageLayout userLink={userLink} />; } // 現(xiàn)在态鳖,我們有這樣的組件: <Page user={user} avatarSize={avatarSize} /> // ... 渲染出 ... <PageLayout userLink={...} /> // ... 渲染出 ... <NavigationBar userLink={...} /> // ... 渲染出 ... {props.userLink}
錯(cuò)誤邊界
部分 UI 的 JavaScript 錯(cuò)誤不應(yīng)該導(dǎo)致整個(gè)應(yīng)用崩潰,為了解決這個(gè)問題恶导,React 16 引入了該概念浆竭;
錯(cuò)誤邊界是一種 React 組件,這種組件可以捕獲并打印發(fā)生在其子組件樹任何位置的 JavaScript 錯(cuò)誤惨寿,并且它會(huì)渲染出備用 UI邦泄,而不是渲染那些崩潰了的子組件樹。錯(cuò)誤邊界在渲染期間裂垦、生命周期方法和整個(gè)組件樹的構(gòu)造函數(shù)中捕獲錯(cuò)誤
- 組件首字母必須大寫
import React from 'react';
import { PhotoStory, VideoStory } from './stories';
const components = {
photo: PhotoStory,
video: VideoStory
};
function Story(props) {
// 錯(cuò)誤!!!虎韵!JSX 類型不能是一個(gè)表達(dá)式。
return <components[props.storyType] story={props.story} />;
// 正確!!!缸废!JSX 類型可以是大寫字母開頭的變量。
const SpecificStory = components[props.storyType];
return <SpecificStory story={props.story} />;
}
- 傳遞子集動(dòng)態(tài)變量(直接傳就行) 在此基礎(chǔ)上改
render() {
const celsius = scale === 'f' ? tryConvert(temperature, toCelsius) : temperature;
const fahrenheit = scale === 'c' ? tryConvert(temperature, toFahrenheit) : temperature;
const tmp = [
{scale: 'c', key: celsius},
{scale: 'f', key: fahrenheit},
]
return (
<div>
{
tmp.map(item => {
return <TemperatureInput
scale={item.scale}
temperature={item.key}
onTemperatureChange={this.handelCommon}
/>
})
}
</div>
);
}
- 傳遞當(dāng)前event
<!--react-->
<input
type="text"
placeholder="Search..."
value={this.props.filterText}
onChange={(e)=>this.handleFilterTextChange('sth', e)}
/>
<!--vue-->
<searchInput
v-model="params.nameKey"
placeholder="搜索名稱"
icon="android-close"
@search="searchData"
@clear="afterChange('nameKey', $event)"
></searchInput>
- Portal 彈窗??
Portal 提供了一種將子節(jié)點(diǎn)渲染到存在于父組件以外的 DOM 節(jié)點(diǎn)的優(yōu)秀的方案。
典型用例是當(dāng)父組件有 overflow: hidden 或 z-index 樣式時(shí)企量,但你需要子組件能夠在視覺上“跳出”其容器测萎。例如,對話框届巩、懸浮卡以及提示框
19.componentWillReceiveProps
使用時(shí)最好比較兩個(gè)props一面陷入死循環(huán)硅瞧,不停更新組件參考
等等。恕汇。腕唧。
參考:
結(jié)語:
有咩有小伙伴能提供個(gè)更恰當(dāng)?shù)奈恼旅滞邸qⅰT娼印聡隆H鼻础F鹈麖U