当前位置: 移动技术网 > IT编程>开发语言>JavaScript > 如何使用24行JavaScript代码实现Redux

如何使用24行JavaScript代码实现Redux

2019年11月16日  | 移动技术网IT编程  | 我要评论

作者:yazeed bzadough
译者:小维fe
原文:freecodecamp

为了保证文章的可读性,本文采用意译而非直译。

90%的规约,10%的库。
redux是迄今为止创建的最重要的javascript库之一,灵感来源于以前的艺术比如fluxelm,redux通过引入一个包含三个简单要点的可伸缩体系结构,使得javascript函数式编程成为可能。如果你是初次接触redux,可以考虑先阅读。

1. redux大多是规约

考虑如下这个使用了redux架构的简单的计数器应用。如果你想跳过的话可以直接查看github repo

1.1 state存储在一棵树中

该应用程序的状态看起来如下:

const initialstate = { count: 0 };

1.2 action声明状态更改

根据redux规约,我们不直接修改(突变)状态。

// 在redux应用中不要做如下操作
state.count = 1;

相反,我们创建在应用中用户可能用到的所有行为。

const actions = {
  increment: { type: 'increment' },
  decrement: { type: 'decrement' }
};

1.3 reducer解释行为并更新状态

在最后一个架构部分我们叫做reduer,其作为一个纯函数,它基于以前的状态和行为返回状态的新副本。

  • 如果increment被触发,则增加state.count
  • 如果decrement被触发,则减少state.count
const countreducer = (state = initialstate, action) => {
  switch (action.type) {
    case actions.increment.type:
      return {
        count: state.count + 1
      };

    case actions.decrement.type:
      return {
        count: state.count - 1
      };

    default:
      return state;
  }
};

1.4 目前为止还没有redux

你注意到了吗?到目前为止我们甚至还没有接触到redux库,我们仅仅只是创建了一些对象和函数,这就是为什么我称其为"大多是规约",90%的redux应用其实并不需要redux。

2. 开始实现redux

要使用这种架构,我们必须要将它放入到一个store当中,我们将仅仅实现一个函数:createstore。使用方式如下:

import { createstore } from 'redux'

const store = createstore(countreducer);

store.subscribe(() => {
  console.log(store.getstate());
});

store.dispatch(actions.increment);
// logs { count: 1 }

store.dispatch(actions.increment);
// logs { count: 2 }

store.dispatch(actions.decrement);
// logs { count: 1 }

下面这是我们的初始化样板代码,我们需要一个监听器列表listeners和reducer提供的初始化状态。

const createstore = (yourreducer) => {
    let listeners = [];
    let currentstate = yourreducer(undefined, {});
}

无论何时某人订阅了我们的store,那么他将会被添加到listeners数组中。这是非常重要的,因为每次当某人在派发(dispatch)一个动作(action)的时候,所有的listeners都需要在此次事件循环中被通知到。调用yourreducer函数并传入一个undefined和一个空对象将会返回一个initialstate,这个值也就是我们在调用store.getstate()时的返回值。既然说到这里了,我们就来创建这个方法。

2.1 store.getstate()

这个函数用于从store中返回最新的状态,当用户每次点击一个按钮的时候我们都需要最新的状态来更新我们的视图。

const createstore = (yourreducer) => {
    let listeners = [];
    let currentstate = yourreducer(undefined, {});
    
    return {
        getstate: () => currentstate
    };
}

2.2 store.dispatch()

这个函数使用一个action作为其入参,并且将这个actioncurrentstate反馈给yourreducer来获取一个新的状态,并且dispatch方法还会通知到每一个订阅了当前store的监听者。

const createstore = (yourreducer) => {
  let listeners = [];
  let currentstate = yourreducer(undefined, {});

  return {
    getstate: () => currentstate,
    dispatch: (action) => {
      currentstate = yourreducer(currentstate, action);

      listeners.foreach((listener) => {
        listener();
      });
    }
  };
};

2.3 store.subscribe(listener)

这个方法使得你在当store接收到一个action的时候能够被通知到,可以在这里调用store.getstate()来获取最新的状态并更新ui。

const createstore = (yourreducer) => {
  let listeners = [];
  let currentstate = yourreducer(undefined, {});

  return {
    getstate: () => currentstate,
    dispatch: (action) => {
      currentstate = yourreducer(currentstate, action);

      listeners.foreach((listener) => {
        listener();
      });
    },
    subscribe: (newlistener) => {
      listeners.push(newlistener);

      const unsubscribe = () => {
        listeners = listeners.filter((l) => l !== newlistener);
      };

      return unsubscribe;
    }
  };
};

同时subscribe函数返回了另一个函数unsubscribe,这个函数允许你当不再对store的更新感兴趣的时候能够取消订阅。

3. 整理代码

现在我们添加按钮的逻辑,来看看最后的源代码:

// 简化版createstore函数
const createstore = (yourreducer) => {
  let listeners = [];
  let currentstate = yourreducer(undefined, {});

  return {
    getstate: () => currentstate,
    dispatch: (action) => {
      currentstate = yourreducer(currentstate, action);

      listeners.foreach((listener) => {
        listener();
      });
    },
    subscribe: (newlistener) => {
      listeners.push(newlistener);

      const unsubscribe = () => {
        listeners = listeners.filter((l) => l !== newlistener);
      };

      return unsubscribe;
    }
  };
};

// redux的架构组成部分
const initialstate = { count: 0 };

const actions = {
  increment: { type: 'increment' },
  decrement: { type: 'decrement' }
};

const countreducer = (state = initialstate, action) => {
  switch (action.type) {
    case actions.increment.type:
      return {
        count: state.count + 1
      };

    case actions.decrement.type:
      return {
        count: state.count - 1
      };

    default:
      return state;
  }
};

const store = createstore(countreducer);

// dom元素
const incrementbutton = document.queryselector('.increment');
const decrementbutton = document.queryselector('.decrement');

// 给按钮添加点击事件
incrementbutton.addeventlistener('click', () => {
  store.dispatch(actions.increment);
});

decrementbutton.addeventlistener('click', () => {
  store.dispatch(actions.decrement);
});

// 初始化ui视图
const counterdisplay = document.queryselector('h1');
counterdisplay.innerhtml = parseint(initialstate.count);

// 派发动作的时候跟新ui
store.subscribe(() => {
  const state = store.getstate();

  counterdisplay.innerhtml = parseint(state.count);
});

我们再次看看最后的视图效果:

原文:

4. 交流

本篇主要简单了解下redux的三个架构组成部分以及如何实现一个简化版的redux,对redux能有进一步的了解,希望能和大家相互讨论技术,一起交流学习。

文章已同步更新至github博客,若觉文章尚可,欢迎前往star!

你的一个点赞,值得让我付出更多的努力!

逆境中成长,只有不断地学习,才能成为更好的自己,与君共勉!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网