React 编写项目连环套路
搭建一个项目
公共部分放一块:index.js style.js store.js reducer.js actionCreateors.js actionTypes.js
分页 一块一块放 拆分在 compentns 里面存放 index.js style.js store.js reducer.js actionCreateors.js actionTypes.js
路由 exact 不会向后取值 不会影响到路由触发
分模块管理
/index
import React,{Component} from 'react'; import {
HomeWapper,
HomeLeft,
HomeRight
} from './style'; import List from './components/List';
import Topic from './components/Topic';
import Recommend from './components/Recommend';
import Writer from './components/Writer'; class Home extends Component{
render(){
return( <HomeWapper>
<HomeLeft>
<img className="banner-img" src="https://upload.jianshu.io/admin_banners/web_images/4592/22f5cfa984d47eaf3def6a48510cc87c157bf293.png?imageMogr2/auto-orient/strip|imageView2/1/w/1250/h/540"/>
<Topic/>
<List/>
</HomeLeft>
<HomeRight>
<Recommend/>
<Writer/>
</HomeRight>
</HomeWapper>
)
}
} export default Home;
/style
import styled from 'styled-components';
import logo from '../../statics/logo.png'; export const HeaderWrapper = styled.div`
position:relative;
width:100%;
height:58px;
border-bottom:1px solid #f0f0f0;
`;
Store(只有一个,reducer有多个)
import {createStore , compose , applyMiddleware } from 'redux';
import reducer from './reducer';
import thunk from 'redux-thunk'; const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
const store = createStore(reducer,composeEnhancers( applyMiddleware(thunk) )); export default store;
Reducer(分块创建 ,最后合并为一个)
import { fromJS } from 'immutable'; const defaultState = fromJS({
topic:[
{
id:1,
title:'社会热点',
imgUrl:'https://s.cn.bing.net/th?id=OJ.hCd0aXzY79mTlQ&w=75&h=75&pid=MSNJVFeeds',
},
{
id:2,
title:'新闻联播',
imgUrl:'https://s.cn.bing.net/th?id=OJ.hCd0aXzY79mTlQ&w=75&h=75&pid=MSNJVFeeds',
},
{
id:3,
title:'项目咨询',
imgUrl:'https://s.cn.bing.net/th?id=OJ.hCd0aXzY79mTlQ&w=75&h=75&pid=MSNJVFeeds',
}
]
}); export default (state = defaultState,action)=>{
switch (action.type){
case:
return; default:
return state; } } ;
actionCreateors(创建action状态的文件)
import * as actionTypes from './actionTypes';
import { fromJS } from 'immutable';
import axios from 'axios'; const changeList = (data) => ({
type: actionTypes.CHANGELIST,
data: fromJS(data),
totalpage: Math.ceil(data.length / 10)
}); export const inputFocus = ()=>({
type:actionTypes.INPUTFOCUS
}); export const inputBlur = () =>({
type:actionTypes.INPUTBLUR
}); export const mouseEnter= () =>({
type:actionTypes.MOUSEENTER
}); export const mouseLeave = () =>({
type:actionTypes.MOUSELEAVE
}); export const changePage = (page) =>({
type:actionTypes.CHANGEPAGE,
page
}) // 异步请求数据 获取搜索框里面的数据
export const getList = () =>{
return(dispath) =>{
axios.get('/api/headerList.json').then((res)=>{
const data = res.data;
dispath(changeList(data.listData)); }).catch(()=>{
console.log('err:1','msg:not found 404');
});
}
};
actionTypes(action常量定义文件)
export const INPUTFOCUS = 'header/INPUTFOCUS';
export const INPUTBLUR = 'header/INPUTBLUR';
export const CHANGELIST = 'header/CHANGELIST';
export const MOUSEENTER = 'header/MOUSEENTER';
export const MOUSELEAVE = 'header/MOUSELEAVE';
export const CHANGEPAGE = 'header/CHANGEPAGE';
记得写”命名空间“
connect 连接props数据(
mapStateToProps,
mapDispatchToProps
)
数据 state
方法路径dispath
const mapStateToProps = (state)=>{
return{ focus:state.getIn(['header','focus']),
list :state.getIn(['header','list']),
page:state.getIn(['header','page']),
show:state.getIn(['header','show']),
totalpage:state.getIn(['header','totalpage'])
}
} const mapDispatchToProps = (dispatch)=>{
return{ handleMouseLeave(){
dispatch(actionCreators.mouseLeave());
}, handleChangePage(page,totalpage,spin){ let rotate = spin.style.transform.replace(/[^0-9]/ig,''); if(rotate){
rotate = parseInt(rotate,10);
}else{
rotate = 0;
} spin.style.transform = 'rotate(' + (rotate + 1000) + 'deg)'; if(page < totalpage){
dispatch(actionCreators.changePage(page + 1));
}else{
dispatch(actionCreators.changePage(1));
} } }
} export default connect(mapStateToProps,mapDispatchToProps)(Header);
React 编写项目连环套路的更多相关文章
- 【腾讯Bugly干货分享】React Native项目实战总结
本文来自于腾讯bugly开发者社区,非经作者同意,请勿转载,原文地址:http://dev.qq.com/topic/577e16a7640ad7b4682c64a7 “8小时内拼工作,8小时外拼成长 ...
- React Native 项目运行在 Web 浏览器上面
React Native 的出现,让前端工程师拥有了使用 JavaScript 编写原生 APP 的能力.相比之前的 Web app 来说,对于性能和用户体验提升了非常多. 但是 React Nati ...
- React 实践项目 (二)
React在Github上已经有接近70000的 star 数了,是目前最热门的前端框架.而我学习React也有一段时间了,现在就开始用 React+Redux 进行实战! React 实践项目 (一 ...
- react实战项目开发(2) react几个重要概念以及JSX语法
前言 前面我们已经学习了利用官方脚手架搭建一套可以应用在生产环境下的React开发环境.那么今天这篇文章主要先了解几个react重要的概念,以及讲解本文的重要知识JSX语法 React重要概念 [思想 ...
- Expo大作战(三)--针对已经开发过react native项目开发人员有针对性的介绍了expo,expo的局限性,开发时项目选型注意点等
简要:本系列文章讲会对expo进行全面的介绍,本人从2017年6月份接触expo以来,对expo的研究断断续续,一路走来将近10个月,废话不多说,接下来你看到内容,讲全部来与官网 我猜去全部机翻+个人 ...
- React 实践项目 (三)
React在Github上已经有接近70000的 star 数了,是目前最热门的前端框架.而我学习React也有一段时间了,现在就开始用 React+Redux 进行实战! 上回说到使用Redux进行 ...
- React 实践项目 (五)
React在Github上已经有接近70000的 star 数了,是目前最热门的前端框架.而我学习React也有一段时间了,现在就开始用 React+Redux 进行实战! React 实践项目 (一 ...
- React Native 项目实战-Tamic
layout: post title: React Native 项目实战 date: 2016-10-18 15:02:29 +0800 comments: true categories: Rea ...
- 如何使用npm构建一个react demo项目
方法一: 1) 安装node.js环境 点我进入nodejs官网 1.1) 下载LTS(Long term support)版本,安装 1.2) 在cmd中使用以下命令查看node是否安装成功 no ...
随机推荐
- 50倍时空算力提升,阿里云RDS PostgreSQL GPU版本上线
2019年3月19日,阿里云RDS PostgreSQL数据库GPU规格版本正式上线,开启了RDS异构计算并行加速之路.该版本在RDS(关系型数据库服务)的云基础设施层面首次完成了与阿里云异构计算产品 ...
- QLabel添加Click信号
使用自定义label来实现此功能 其他控件可参照此例. #include "customerqlabel.h" CustomerQlabel::CustomerQlabel(QWi ...
- 【转载】ubuntu下编写字符设备驱动程序-入门篇
在ubuntu初学驱动,觉得挺有用的. http://www.eefocus.com/jefby1990/blog/13-02/291628_c39b8.html
- 使用Fiddler抓取到的“姐夫酷”API接口
下午本来准备抓取些网页视频地址,做一个小的视频app,用来学习ijkplayer,无意中发现了一个app--姐夫酷,这是一个很简单的网页,它也有相应的一个比较简单的android app. 于是心血来 ...
- R语言因子
R语言因子 因子是它们用于将数据进行分类并将其存储为级别的数据对象.它们可以同时存储字符串和整数.它们在具有唯一值的有限数目的列是有用的. 例如,"male, "Female&qu ...
- django其他
聚合查询 聚合函数必须在分组之后使用 没有分组默认整体为一组 聚合函数 Max, Min, Sum, Avg, Count 从django.db.models 导入方法,然后使用关键字aggregat ...
- vue2-vux-fitness-project
非常感谢那些无私开源的程序员,希望我也能够有能力像你们那样,开源很多很有意思的东西~~ //index.html <!DOCTYPE html> <html> <head ...
- webpack配置vue项目
npm init 安装webpack,webpack-cli 新建src目录,在src目录下新建main.js
- @bzoj - 4951@ [Wf2017]Money for Nothing
目录 @description@ @solution@ @accepted code@ @details@ @description@ 在这道题种你需要解决一个全世界人类从存在起就在面临的最深刻的问题 ...
- hdu4318 最短路变形
和hdu有一题差不多.给的是损失比,1-c%就是保存了多少,找出最大的保存率即可. #include<stdio.h> #include<iostream> #include& ...