3.4.3 • Published 4 years ago

dace-plugin-redux v3.4.3

Weekly downloads
10
License
ISC
Repository
github
Last release
4 years ago

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>
    );
  }
}
3.4.3

4 years ago

3.4.2

4 years ago

3.4.1

4 years ago

3.4.0

4 years ago

3.3.8

4 years ago

3.3.7

4 years ago

3.3.5

4 years ago

3.3.4

4 years ago

3.3.3

4 years ago

3.3.2

4 years ago

3.3.1

4 years ago

3.3.0

4 years ago

3.2.1

4 years ago

3.2.0

4 years ago

3.1.2

4 years ago

3.1.1

4 years ago

3.1.0

4 years ago

3.0.0

4 years ago

2.2.0

5 years ago

2.1.5

5 years ago

2.1.4

5 years ago

2.1.3

5 years ago

2.1.2

5 years ago

2.1.1

5 years ago

2.1.0

5 years ago

2.0.8

5 years ago

2.0.6

5 years ago

2.0.5

5 years ago

2.0.4

5 years ago

2.0.3

5 years ago

2.0.2

5 years ago

2.0.1

5 years ago

2.0.0

5 years ago

2.0.0-alpha.7

5 years ago

2.0.0-alpha.6

5 years ago

2.0.0-alpha.5

5 years ago

2.0.0-alpha.4

5 years ago

2.0.0-alpha.3

5 years ago

2.0.0-alpha.2

5 years ago

2.0.0-alpha.1

5 years ago

2.0.0-alpha.0

5 years ago

1.11.0

5 years ago

1.10.0

5 years ago

1.9.0

5 years ago

1.8.2

5 years ago

1.8.1

5 years ago

1.8.0

5 years ago

1.7.1

5 years ago

1.7.0

5 years ago

1.6.2

5 years ago

1.6.1

6 years ago

1.6.0

6 years ago

1.5.3

6 years ago

1.5.2

6 years ago

1.5.1

6 years ago

1.5.0

6 years ago

1.4.0

6 years ago

1.3.1

6 years ago

1.3.0

6 years ago

1.2.1

6 years ago

1.2.0

6 years ago

1.1.2

6 years ago

1.1.1

6 years ago

1.1.0

6 years ago

1.0.14

6 years ago

1.0.13

6 years ago

1.0.12

6 years ago

1.0.11

6 years ago

1.0.10

6 years ago

1.0.9

6 years ago

1.0.8

6 years ago

1.0.7

6 years ago

1.0.6

6 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago