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