import React from 'react' import ReactDom from 'react-dom' import App from './App' ReactDom.render( <App/>, document.getElementById('root') )
沒什麼好說的react
import React from 'react' import { createStore, applyMiddleware } from 'redux' import { Provider } from 'react-redux' import thunk from 'redux-thunk' import reducers from './reducers' import RouterMap from './router' let store = createStore(reducers, applyMiddleware(thunk)) const App = () => { return ( <Provider store={ store }> <RouterMap/> </Provider> ) } export default App
代碼分析:ios
import React from 'react' // react-router 相關 import { Router, Route, Switch } from 'react-router-dom' // 瀏覽器的History模式 import createHistory from 'history/createBrowserHistory' // 生成一個redux容器的方法 import { connect } from 'react-redux' // 一個action import { initCity } from '../actions/userinfo' // 組件 import Home from '../containers/Home' import City from '../containers/City' // 建立 history const history = createHistory() class App extends React.Component { render() { return ( <Router history={ history }> <Switch> <Route exact path="/" component={ Home }></Route> <Route path="/city" component={ City }></Route> </Switch> </Router> ) } componentDidMount() { this.props.initCity(cityName) } } function mapStateToProps(state) { return { userInfo: state.userInfo } } function mapDispatchToProps(dispatch, ownprops) { return { initCity: (cityName) => { } } } export default connect( mapStateToProps, mapDispatchToProps )(App)
代碼分析:ajax
import { combineReducers } from 'redux' export function detailInfo(state = {}, action) { switch (action.type) { case 'GET_DETAIL_INFO': return { ...state, info: action.payload.info } case 'GET_COMMENT_LIST': return { ...state, comments: action.payload.comments } default: return state } } const initialState = {page: 0, likeList: [] } export function userInfo(state = initialState, action) { switch (action.type) { case 'USER_CURRENTCITY': return { ...state, cityName: action.payload.cityName, userName: '小名' } case 'SAVE_HOMEAD': return { ...state, homeAd: action.payload.homeAd } case 'SAVE_LIKELIST': return { ...state, page: state.page + 1, likeList: state.likeList.concat(action.payload.likeList) } case 'SET_ISLOADINGLIKELIST_FLAG': return { ...state, isLoading: action.payload.isLoading } default: return state } } export default combineReducers({ userInfo, detailInfo })
代碼分析:combineReducers: 接收一個拆分後 reducer 函數組成的對象,返回一個新的 Reducer 函數redux
import axois from 'axios' export function getDetailInfo(shopId) { return dispatch => axois.get(`/api/detail/info/${shopId}`).then((res) => { dispatch(saveDetailInfo(res.data)) }).catch((error) => { console.log(error) }) } export function saveDetailInfo(res) { return { type: 'GET_DETAIL_INFO', payload: { info: res } } } export function getComments(shopId) { return axois.get(`/api/detail/comment/${shopId}`).then((res) => { dispatch(saveComments(res.data)) }).catch((error) => { console.log(error) }) } export function saveComments(res) { return { type: 'GET_COMMENT_LIST', payload: { comments: res } } }
import React from 'react' import { connect } from 'react-redux' import { updateCity } from '../../actions/userinfo' import Header from '../../components/Header' import CityList from '../../components/CityList' import CurrentCity from './subpages/CurrentCity' class City extends React.Component { render() { return ( <div> <Header title="選擇城市" goBack={ this.goBack.bind(this) }></Header> <CurrentCity currentCityName={ this.props.userInfo.cityName }></CurrentCity> <CityList chooseCity= { this.chooseCity.bind(this) }></CityList> </div> ) } goBack() { this.props.history.goBack() } chooseCity(cityName) { localStore.setItem('USER_CURRENT_CITY', cityName) this.props.initCity(cityName) this.props.history.goBack() } } function mapStateToProps(state) { return { userInfo: state.userInfo } } function mapDispatchToProps(dispatch, ownProps) { return { initCity: (cityName) => { dispatch(updateCity(cityName)) } } } export default connect( mapStateToProps, mapDispatchToProps )(City)
goBack={ this.goBack.bind(this) } : 將組件自己的goBack方法,傳遞給 Header 組件 , bind的做用是:在Header組件調用該方法時,上下文爲City組件的 thisaxios
import React from 'react' class Header extends React.Component { render() { return ( <div id="common-header"> <span className="back-icon" onClick={ this.clickHandle.bind(this) }> <i className="icon-chevron-left"></i> </span> <h1>{ this.props.title }</h1> </div> ) } clickHandle() { window.history.back() } } export default Header
Header組件並無用connect包裝,由於它是純顯示的組件,只有跟業務相關的組件,纔會用connect容器包裝api