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> })
componentWillMount 在渲染前調用,在客戶端也在服務端。
componentDidMount : 在第一次渲染後調用,只在客戶端。
componentWillReceiveProps 在組件接收到一個新的 prop (更新後)時被調用。這個方法在初始化render時不會被調用。
shouldComponentUpdate 返回一個布爾值。在組件接收到新的props或者state時被調用。在初始化時或者使用forceUpdate時不被調用。
componentWillUpdate 在組件接收到新的props或者state但尚未render時被調用。在初始化時不會被調用。
componentDidUpdate 在組件完成更新後當即調用。在初始化時不會被調用。
componentWillUnmount 在組件從 DOM 中移除以前馬上被調用。
<input type="text" ref="myInput" /> this.refs.myInput.focus();
//傳參和收參 [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'));
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> ); } }
npm i sass-loader node-sass --save-dev