[前端打怪升級--LV.1] react的基礎內容

react使用教程

變量的使用

constructor(props) {
super(props);
this.state = {
sliderSwiper: null,
movies: []
};
this.handleStart = this.handleStart.bind(this);
}

if (!baseHref) {
if (isPc) {
window['location']['href'] = `${location.protocol}//${location.host}/demo/home`;
return;
}
if (!isPc) {
window['location']['href'] = `${location.protocol}//${location.host}/m/home`;
return;
}
} else {
if (isPc && baseHref === 'm') {
window['location']['href'] = `${location.protocol}//${location.host}/demo/home`;
return;
}
if (!isPc && baseHref === 'demo') {
window['location']['href'] = `${location.protocol}//${location.host}/m/home`;
return;
}
}

父組件向子組件傳遞參數

this.props.xxx

子向父傳遞參數

//父組件
<ComentList arr={this.state.arr} pfn={this.fn.bind(this)}>
//子組件
clickFun(text) {
        this.props.pfn(text)//這個地方把值傳遞給了props的事件當中
    }
<button onClick={this.clickFun.bind(this, this.state.childText)}>
                    click me
 </button>

組件判斷渲染

const isLoggedIn = this.state.isLoggedIn; 
let button = null; 
if (isLoggedIn) { 
button = <LogoutButton onClick={this.handleLogoutClick} />; 
} else { 
button = <LoginButton onClick={this.handleLoginClick} />; 
} 
return ( 
<div> 
<Greeting isLoggedIn={isLoggedIn} /> 
{button} 
</div> ); 
}

組件循環渲染

arr.map((element,index) =>{
    return <div>{index}</div>
})

react生命週期

componentWillMount 在渲染前調用,在客戶端也在服務端。
componentDidMount : 在第一次渲染後調用,只在客戶端。
componentWillReceiveProps 在組件接收到一個新的 prop (更新後)時被調用。這個方法在初始化render時不會被調用。
shouldComponentUpdate 返回一個布爾值。在組件接收到新的props或者state時被調用。在初始化時或者使用forceUpdate時不被調用。
componentWillUpdate 在組件接收到新的props或者state但尚未render時被調用。在初始化時不會被調用。
componentDidUpdate  在組件完成更新後當即調用。在初始化時不會被調用。
componentWillUnmount 在組件從 DOM 中移除以前馬上被調用。

refs的使用 和vue相似

<input type="text" ref="myInput" />
this.refs.myInput.focus();

react 路由的使用

//傳參和收參
[react跳轉和參數接收](https://blog.csdn.net/qq_24504591/article/details/78973633)

// routers.js
import React, { Component } from 'react';
import { HashRouter as Router, Route, Switch, Redirect} from 'react-router-dom';
import Home from './views/Home'
import List from './views/List';
import Board from './views/Board';
import Item from './views/Item';
import Search from './views/Search';
class Routes extends Component {
render() {
return (
<div className="App">
<Router>
<Switch>
<Route path="/home" component={Home}></Route>
<Route path="/board" component={Board}></Route>
<Route path="/list" component={List}></Route>
<Route path="/item" component={Item}></Route>
<Route path="/search" component={Search}></Route>
<Redirect from="/" to="/home"></Redirect>
<Route component={Home}></Route>
</Switch>
</Router>
</div>
);
}
}
export default Routes;

// 如何跳轉
<Link to={`item?id=${item.id}`} ></Link>

<Link to={`/list?type=${item.key.split(',')[0]}&title=${item.title}`}></Link>

<NavLink to="/search" className="nav-link"></NavLink>

// index.js中
import Routes from './routes';
ReactDOM.render(<Routes />, document.getElementById('root'));

react組件種類

  1. 靜態組件
function HelloMessage(props) { return <h1>Hello {props.name}!</h1>; }

2.動態組件css

class List extends Component {
render() {
return (
<div className="container">
<MovieList type="more-list"/>
</div>
);
}
}

z項目中使用scss

npm i sass-loader node-sass --save-dev
相關文章
相關標籤/搜索