Giter Club home page Giter Club logo

redux-wrap's Introduction

说明

模仿了之前的tarot的接口,整合action和reducers,使每个项目只需要一个model.js文件就可以书写逻辑

用法

start({root, component, models<Object|Array>})

入口,创建store,renderdom,初始化store,

  • root: dom节点,根节点
  • component: react组件
  • models: 数据model

connect(component)

用法基本和react-redux的相同,只不过不需要传递mapstate和mapdispatch,默认全部传递

dispatch({type: 'testPage/changeName', payload: {name: 'xxx'}})

使用connect包装过的组件,会在props拥有dispatch方法,而且不需要写action creator,type类型用/分开,斜杠前是model的name,斜杠后是方法的名称, payload会在对应的方法的第二个参数出现

例子:

import React from 'react';
import Home from '../container';

import {start} from '../../common/stores';

import model from '../model';

start({
	root: document.getElementById('root'),
	component: <Home />,
	model: [model]
});

models例子:

export default {
	name: 'testPage',
	data: {
		name: 'lingyun',
		age: '18'
	},
	sync: {
		changeName: (state, payload) => {
			return {
				name: payload.name
			};
		}
	},
	async: {
		changeAge: (dispatch, getState, payload) => {
			setTimeout(() => {
				dispatch({
					type: 'changeName',
					payload: {
						name: payload.name
					}
				});
			}, 2000);
		}
	}
};

注意:有一点一定要注意,sync和async方法,不要重名,否则只会调用async里的方法。

组件使用例子:

import React, {Component} from 'react';

import {connect} from "../../common/stores";

class Home extends Component {
	constructor(props) {
		super(props);

		this.handleClick = this.handleClick.bind(this);
	}

	shouldComponentUpdate() {
		return true;
	}

	handleClick() {
		this.props.dispatch({
			type: 'testPage/changeName',
			payload: {
				name: 'iii'
			}
		});
		this.props.dispatch({
			type: 'testPage/changeAge',
			payload: {
				name: 'kkkkkk'
			}
		});
	}

	render() {

		return (
			<div>
				<div>
					{this.props.testPage.name}
				</div>
				<button
					onClick={this.handleClick}
					type="button"
				>
					我是大按钮
				</button>
			</div>
		);
	}
}

export default connect(Home);

redux-wrap's People

Contributors

lingyunruo avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.