npm install --save style-components

https://www.jianshu.com/p/27788be90605(copy)

"axios": "^0.18.0",
"immutable": "^3.8.2",                                 //不可修改变量插件
"react": "^16.4.0",
"react-dom": "^16.4.0",
"react-loadable": "^5.4.0",
"react-redux": "^5.0.7",
"react-router": "^4.3.1",
"react-router-dom": "^4.3.1",                      // 路由
"react-scripts": "1.1.4",
"react-transition-group": "^2.3.1",                        //动画
"redux": "^4.0.0",
"redux-immutable": "^4.0.0",                              
"redux-thunk": "^2.3.0",                                       //允许action返回函数
"styled-components": "^3.3.2"                           //全局css

创建完基础框架

写完一个模块,然后创建store,验证store,然后对store进行优化

涉及到Redux Devtool 和 reducer的分模块化

拆分actionCreators和constants的js文件

npm install immutable

npm install redux-immutable --save

npm install react-redux --save

npm install react-router --save

npm install redux-thunk --save

npm install styled-components --save

npm install  react-router-dom --save

创建全局sotre

sotre/index.js

import {createStore,compose, applyMiddleware} from 'redux';
import thunk from 'redux-thunk';
import reducer from './reducer';
const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
const store=createStore(reducer, composeEnhancers(
applyMiddleware(thunk)
));
export default store;

sotre/reducer.js

import {combineReducers} from 'redux-immutable'
// import {reducer as headerReducer} from '../common/header/store';
const reducer= combineReducers({
// header:headerReducer,
});
export default reducer;

app.js添加路由

import Home from './pages/home';
import Shop from './pages/shop';
import Car from './pages/car';
import User from './pages/user';
   <BrowserRouter>
<Route path='/' exact component={Home}></Route>
<Route path='/shop' exact component={Shop}></Route>
<Route path='/car' exact component={Car}></Route>
<Route path='/user' exact component={User}></Route>
</BrowserRouter>

每个模块目录结构,先创建index.js文件

home/index.js

import React, { PureComponent } from 'react';
class Home extends PureComponent {
render() {
return (
<div>Home</div>
)
}
componentDidMount() {
}
}
export default Home;

如果有公共组件

创建common文件夹

编写style.js文件(相当于css)

import styled from 'styled-components';
export const HeaderWrapper=styled.div`
height:58px;
background-color: #fff;
border-bottom:1px solid #f0f0f0;
`;

引入style.js

import {
HeaderWrapper,
Logo,
Nav,
NavItem,
NavSearch,
} from './style';

nav导航添加链接

import { Link } from 'react-router-dom';
<Link to={'/detail/' + item.get('id')}>
<div className='nav-item flexca'>商城</div>
</Link>

sotre  连接专题

先修改APP.js,把 Provider放开,引入sotre

import React, { Fragment, Component } from 'react';
import { Provider } from 'react-redux';
import { GlobalStyle } from "./style";
import { BrowserRouter, Route } from 'react-router-dom';
import TabBar from './common/tabBar';
import Home from './pages/home';
import Shop from './pages/shop';
import Car from './pages/car';
import User from './pages/user';
import store from './store';
class App extends Component {
render() {
return (
<Fragment>
<GlobalStyle />
<Provider store={store}>
<BrowserRouter>
<Route path='/' exact component={Home}></Route>
<Route path='/shop' exact component={Shop}></Route>
<Route path='/car' exact component={Car}></Route>
<Route path='/user' exact component={User}></Route>
<TabBar />
</BrowserRouter>
</Provider>
</Fragment>
);
}
}
export default App;

先修改模块主js文件,引入connect,actionCreators

import React, { PureComponent } from 'react';
import { connect } from 'react-redux';
import { actionCreators } from './store';
import { Link } from 'react-router-dom';
import {
NavBar,
} from './style';
class TabBar extends PureComponent {
render() {
const { setBackColor,navNumber } = this.props;
console.log(navNumber )
//直接函数setBackColor,传参()=>setBackColor(0)
return ( <NavBar className='flex'>

        {/* <Link className='nav-item flexca' to={'/'}  onClick={setBackColor(0)}> */}
{/* 商城 */}
{/* </Link> */}
<Link className={`nav-item flexca ${0===navNumber?"active":null}`} to={'/'} onClick={()=>setBackColor(0)}>
商城
</Link>
<Link className={`nav-item flexca ${1===navNumber?"active":null}`} to={'/shop'} onClick={()=>setBackColor(1)}>
分类
</Link>
<Link className={`nav-item flexca ${2===navNumber?"active":null}`} to={'/car'} onClick={()=>setBackColor(2)}>
购物车
</Link>
<Link className={`nav-item flexca ${3===navNumber?"active":null}`} to={'/user'} onClick={()=>setBackColor(3)}>
我的
</Link>
      </NavBar>
)
}
componentDidMount() {
}
// setBackColor(){
// console.log(1)
// }
}
const mapState = (state) => ({
navNumber: state.getIn(['tabBar', 'navNumber'])
}); const mapDispatch = (dispatch) => ({
setBackColor(number) {
console.log(1111)
dispatch(actionCreators.setBackColor(number))
}
});
export default connect(mapState, mapDispatch)(TabBar);

reducer.js

import {combineReducers} from 'redux-immutable'
import {reducer as tabBarReducer} from '../common/tabBar/store';
const reducer= combineReducers({
tabBar:tabBarReducer,
});
export default reducer;

index.js

import reducer from './reducer';
import * as actionCreators from './actionCreators';
import * as constants from './constants'; export {reducer,actionCreators,constants}

constants.js

export const NAV_SELECT = 'tabBar/NAV_SELECT';

actionCreators.js

// import axios from 'axios';
import * as constants from './constants';
// import { fromJS } from 'immutable';
//*执行完去找reducer
export const setBackColor = (number) => ({
  type: constants.NAV_SELECT,
  number
})

成功后,浏览器这样变化

引入immutable模块,'模块'中的store/reducer文件引入immutable来锁定state

引入redux-immutable模块,'项目'store//reducer文件引入redux-immutable

注意:immutable   模块 fromJS(data)处理过的数据已经不是普通的数据,不许与相同类型的数据进行组合,

并且成功后页面表象没有任何编号,但数据确实是全新的数据,不是原始state,注意immutable 流和redux流

如果页面没报错,数据还没出来,就改怀疑有没有 数据.toJS();这样才能把数据转为普通js数据,才能执行js函数

实战build-react(三)+ style-components的更多相关文章

  1. 【转】Facebook React 和 Web Components(Polymer)对比优势和劣势

    原文转自:http://segmentfault.com/blog/nightire/1190000000753400 译者前言 这是一篇来自 StackOverflow 的问答,提问的人认为 Rea ...

  2. Facebook React 和 Web Components(Polymer)对比优势和劣势

    目录结构 译者前言 Native vs. Compiled 原生语言对决预编译语言 Internal vs. External DSLs 内部与外部 DSLs 的对决 Types of DSLs - ...

  3. 基于 abp vNext 和 .NET Core 开发博客项目 - Blazor 实战系列(三)

    系列文章 基于 abp vNext 和 .NET Core 开发博客项目 - 使用 abp cli 搭建项目 基于 abp vNext 和 .NET Core 开发博客项目 - 给项目瘦身,让它跑起来 ...

  4. AspNetCore-MVC实战系列(三)之个人中心

    AspNetCore - MVC实战系列目录 . 爱留图网站诞生 . git源码:https://github.com/shenniubuxing3/LovePicture.Web . AspNetC ...

  5. react实战系列 —— React 中的表单和路由的原理

    其他章节请看: react实战 系列 React 中的表单和路由的原理 React 中的表单是否简单好用,受控组件和非受控是指什么? React 中的路由原理是什么,如何更好的理解 React 应用的 ...

  6. 工作经常使用的SQL整理,实战篇(三)

    原文:工作经常使用的SQL整理,实战篇(三) 工作经常使用的SQL整理,实战篇,地址一览: 工作经常使用的SQL整理,实战篇(一) 工作经常使用的SQL整理,实战篇(二) 工作经常使用的SQL整理,实 ...

  7. [SQL SERVER系列]工作经常使用的SQL整理,实战篇(三)[原创]

    工作经常使用的SQL整理,实战篇,地址一览: 工作经常使用的SQL整理,实战篇(一) 工作经常使用的SQL整理,实战篇(二) 工作经常使用的SQL整理,实战篇(三) 接着本系列前面两篇继续讨论. 有时 ...

  8. SAS数据挖掘实战篇【三】

    SAS数据挖掘实战篇[三] 从数据挖掘概念到SAS EM模块和大概的流程介绍完之后,下面的规划是[SAS关联规则案例][SAS聚类][SAS预测]三个案例的具体操作步骤,[SAS的可视化技术]和[SA ...

  9. 手把手和你一起实现一个Web框架实战——EzWeb框架(三)[Go语言笔记]Go项目实战

    手把手和你一起实现一个Web框架实战--EzWeb框架(三)[Go语言笔记]Go项目实战 代码仓库: github gitee 中文注释,非常详尽,可以配合食用 本篇代码,请选择demo3 这一篇文章 ...

  10. [Recompose] Configure Recompose to Build React Components from RxJS Streams

    Recompose provides helper functions to stream props using an Observable library of your choice into ...

随机推荐

  1. 1.LTE系统概述

    信息源:中国大学MOOC 中搜索 移动通信网络与优化 兰州交通大学 3GPP协议:https://www.3gpp.org/DynaReport/36-series.htm 可以在中国大学MOOC中搜 ...

  2. vue ----》实现打印功能

    1.安装打印相关依赖 cnpm install vue-print-nb --save 2.安装后,在main.js文件中引入 import Print from 'vue-print-nb' Vue ...

  3. Maven使用基础

    (转)https://my.oschina.net/xiaomaoandhong/blog/104045 基于 约定优于配置(Convention Over Configuration)的原则,无特殊 ...

  4. 教你用 Netty 实现一个简单的 RPC!

    众所周知,dubbo 底层使用了 Netty 作为网络通讯框架,而 Netty 的高性能我们之前也分析过源码,对他也算还是比较了解了. 今天我们就自己用 Netty 实现一个简单的 RPC 框架. 1 ...

  5. adb 打印kernel输出的log

     一. linux 内核printk机制     1.1. Android内核是基于Linxu kernel的,因此其log机制也是通用的,在Android内核中使用printk函数进行Log输出.与 ...

  6. C++中的数据类模板

    1,预备知识: 1,模板参数可以是数值型参数(非类型参数): 1,代码示例: template <typename T, int N> void func() { T a[N]; // 使 ...

  7. postgresql 用 like 可以 复制结构包括主键约束

    create tabletablename ( like tablename INCLUDING INDEXES INCLUDING COMMENTS); PostgreSQL 动态表复制(CREAT ...

  8. ubuntu18下lamp虚拟路劲配置

    一.配置二级域名 修改hosts文件,模拟dns解析. 位置:/etc/hosts 添加 127.0.0.1  myweb.service.com 二.创建项目目录 apache默认目录是/var m ...

  9. frame的处理

    自动化测试时,有时会定位不到某些元素,是因为这些元素在frame中,所以必须先进入到frame中,才能再去定位要定位的元素. frame是页面的框架,即在一个浏览器的窗口显示多个页面,可以是水平框架和 ...

  10. for XML path 使用技巧

    FOR XML PATH 是sqlserver数据库的语法,能将查询出的数据转换成xml格式的数据. 首先,我们来看一个正常的查询: SELECT TOP 2 id, name,crDate FROM ...