npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

dace-plugin-redux

v3.4.3

Published

让 redux 和 dace 一起工作的插件

Downloads

41

Readme

dace-plugin-redux

该插件让 redux 和 dace 能一起工作。

插件不带数据缓存功能,开发者根据实际业务需求自己来设定缓存策略。

安装

npm i dace-plugin-redux

用法

dace.config.js 的插件中添加配置:

module.exports = {
  plugins: ['redux']
};

参数

特别注意

middlewares 参数中的 js 代码目前只支持 ES5 语法。

使用封装好的 redux 中间件

// 创建 axios 实例的文件路径
DACE_AXIOS_INSTANCE_PATH: 'src/axios.js',

// 加上 dace-plugin-redux
plugins: [
  ['redux', {
    middlewares: [ `require('redux-logger').default` ]
  }]
]

直接在配置中书写 redux 中间件

plugins: [
  ['redux', {
    middlewares: [
      `function() {
        const createLogger = require('redux-logger').createLogger;
        return createLogger();
      }()`
    ]
  }]
]

API

getInitialProps 方法

参数

getInitialProps 接收的上下文对象中除了默认参数外,增加了 store

  • store:这个参数是通过 redux 的 createStore 创建的 store 实例,详情请查阅 redux 官方文档

@getInitialProps 装饰器

非 redux 技术推荐直接使用 getInitialProps 静态方法。

该装饰器主要是为了简化页面组件的编程,让代码看起来更简洁。装饰器做了两件事情:

  • 在两端(服务器端、浏览器端)渲染时,分别执行 injectReducer 来注入页面相关的 reducer。
  • 为页面组件绑定静态方法 getInitialProps

参数

@getInitialProps 接收一个对象参数,对象参数包含以下属性:

  • reducer:和页面 action 相关的 reducer 。
  • promise:需要绑定到静态方法 getInitialProps 的内容。
    • promise.store:页面的 store 对象。
      • promise.store.api:store 对象除了 dispatch()getState() 等常用方法外,还会增加 api ,它是一个 axios 实例,axios.baseURL 取环境变量中的 DACE_API_BASE_URL,当 DACE_API_BASE_URL 为空时,axios.baseURL 取当前域名。当采用服务器端渲染时,api 会透传 request headers 的所有信息。
    • promise.query:网址中的 query string。
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import { Link } from 'dace';
import { getInitialProps } from 'dace-plugin-redux';
import { fetchPosts } from './action';
import reducer from './reducer';
import Layout from '../../layouts/default';

@getInitialProps({
  reducer,
  promise: ({ store }) => store.dispatch(fetchPosts())
})
@connect(state => state)
export default class Index extends Component {
  static propTypes = {
    posts: PropTypes.arrayOf(PropTypes.shape({
      id: PropTypes.number,
      title: PropTypes.string
    }))
  };

  static defaultProps = {
    posts: []
  }

  render() {
    return (
      <Layout>
        <h1>Post List</h1>
        <ol>
          {
            this.props.posts.map(post => (
              <li key={post.id}>
                <Link to={`/post/${post.id}`}>{post.title}</Link>
              </li>
            ))
          }
        </ol>
      </Layout>
    );
  }
}