Coder Social home page Coder Social logo

redux's Introduction

关于redux的基本用法

##首先 npm install redux //引入redux const redux = require('redux');

//将redux的createStore的方法独立出来

const createStore = redux.createStore;

//创建一个对象

const initState = {

state:[]

}

//下面是创建一个函数 function getUser(state,action){

  state = state || initState;

//initState就是不传递参数的时候我们的默认值,执行过一次之后就默认会存在state这个状态了,以后就不会走initState switch(action.type){

 case 1;
 
  return {state:[1,2,3]};
  
  break;
  
  default:
  
  return initState;

}

} //将我们上面创建的函数放置到仓库中

var store = createStore(getUser);

//下面是redux提供的属性组件 当我们执行完store.dispatch(all())这个方法之后执行这个方法

store.subscribe(function(){

const currentState = store.getState();

currentState.state.push(4,5,6);

})

function all(){

return { type:1 } }

store.dispatch(all())//all其实就是一个action,只是这个时候才进行传递

//现在只要我们执行

store.getState()//这个方法就会将所有的状态全部都显示出来

//如果想显示state可以使用

store.getState().state

redux's People

Contributors

33033624 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.