項目環境搭建css
使用create-react-app前端
CSS使用styled-componentsreact
yarn add styled-componentsios
引入reset.css樣式git
import { createGlobalStyle } from 'styled-components'github
import { createStore, applyMiddleware, compose } from 'redux'; + const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose; + const store = createStore(reducer, /* preloadedState, */ composeEnhancers( - const store = createStore(reducer, /* preloadedState, */ compose( applyMiddleware(...middleware) ));
使用combineReducer對reducer拆分redux
修改store下的reducer.js代碼axios
import { combineReducers } from 'redux' import { reducer as headerReducer} from '../components/header/store' export default combineReducers({ header: headerReducer })
使用actionCreators.js和actionTypes.js對store代碼優化後端
使用immutable.js和redux-immutable跨域
yarn add redux-immutable
生成的immutable對象,改對象不可改變
yarn add immutable
如何使用
import {fromJS} from 'immutable' const defaultState = fromJS({ focused: false })
const mapStateToProps = state => { return { focused:state.header.get('focused') } }
export default (state = defaultState, action) => { if (action.type === actionTypes.SEARCH_BLUR) { return state.set('focused', false) } if (action.type === actionTypes.SEARCH_FOCUS) { return state.set('focused', true) } return state }
使用redux-thunk
異步操做不在componentDidMount中操做
放到action
yarn add redux-thunk
後端使用koa koa-router koa-cors mock
模擬後端服務器
前端使用axios
跨域配置 "proxy": "http://localhost:8080"
安裝react-router-dom
<BrowserRouter>
<div>
<Header/>
<Route path="/" exact component={Home}/>
<Route path="/detail/:id" exact component={Detail}/>
</div>
</BrowserRouter>
得到pathname
const {pathname} = this.props.location
頁面跳轉
this.props.history.push('/')
頁面重定向
<Redirect from='/...' to='/...' />