如何在不使用路由的情况下将特定的reducer变量作为props来访问

时间:2017-07-12 12:48:56

标签: javascript reactjs redux

我正在开发react-redux应用程序,我可以通过路由访问reducer。现在我遇到了在不使用路由的情况下访问特定减速器的麻烦。

这是我的reducers.js:

const initialState = {
  loading: false,
  topics: []
};

export default createReducer(initialState, {
  [LOADING_DATA]: (state, action) => {
    return Object.assign({}, state, {
      loading: action.loading
    });
  }
});

这是我的行动.js:

export function loading (loading) {
  return {
    type: LOADING_DATA,
    payload: {loading}
  };
}

这就是我在组件上的内容:

import {connect} from 'react-redux'
import {bindActionCreators} from 'redux';
import * as moduleActionCreators from '...';
import * as actionCreators from '...';

class MyComponent extends Component {
  ...
  render () {
     return (<div>
        ...
     </div>;
  }
}

const mapStateToProps = (state) => ({

});

const mapDispatchToProps = (dispatch) => ({
  actions: bindActionCreators(Object.assign({}, moduleActionCreators, actionCreators), dispatch)
});

export default connect(mapStateToProps, mapDispatchToProps)(MyComponent);

通常在mapStateToProps中我将reducer变量引用为loading: state['my_reference_to_reducer'].loading,但我无法弄清楚如何告诉组件引用我的reducers.js以获得{{1}作为道具。

我很欣赏这一点。

1 个答案:

答案 0 :(得分:1)

您需要在mapStateToProps函数中设置状态才能访问它:

const mapStateToProps = (state) => {
  return {
    loading: state.loading
  }
}

然后,您应该可以在MyComponent中将其用作this.props.loading

您的减速机可能如下所示:

export default function reducer(state = {}, action) {
  switch(action.type) {
    case 'LOADING_DATA':
      return Object.assign({}, state, {
        ...state,
        loading: action.payload.loading
      })

我建议您使用redux ducks模式,因为它可以将动作创建者和缩减器保存在同一个文件中,从而节省您的时间并使其更易于阅读和使用。例如:

<强> loading.js

// Actions
const LOADING_DATA   = 'LOADING_DATA'

// Action Creators
export const loadingData = (data) => {
  return {
    type: LOADING_DATA,
    payload: {
      loading: data
    }
  }
}

// Reducer
export default function reducer(state = {
  loading: 'DATA zeroed'
}, action) {
  switch(action.type) {

    case 'LOADING_DATA':
      return Object.assign({}, state, {
        ...state,
        loading: action.payload.loading
    })

    default:
      return state
  }   
}

<强> index.js

import React from 'react';
import ReactDOM from 'react-dom';
import MyComponent from './MyComponent';
import configureStore from './configureStore'

const store = configureStore()

ReactDOM.render(
  <MyComponent store={store}/>, 
  document.getElementById('root')
);

<强> configureStore.js

import { createStore } from 'redux'
import { composeWithDevTools } from 'redux-devtools-extension'
import loadingData from './loading'

const configureStore = () => {
  return createStore(loadingData, composeWithDevTools())
}

export default configureStore

<强> MyComponent.js

import React, { Component } from 'react';
import { connect } from 'react-redux';
import { loadingData } from './loading';

class MyComponent extends Component {
  constructor(props){
    super(props)

    this.onLoadingData = this.onLoadingData.bind(this)
  }

  componentDidMount() {
    this.props.loadingData('no more undefined')
  }

  onLoadingData() {
    this.props.loadingData('DATA')
  }

  render() {
    console.log(this.props.loading)
    return (
      <div>
        <h2>MyComponent</h2>
        <button onClick={this.onLoadingData}>Load Data</button>
        <p>{this.props.loading}</p>
      </div>
    );
  }
}

const mapStateToProps = (state) => {
  return {
    loading: state.loading
  }
}

const mapDispatchToProps = {
  loadingData
}

export default connect(
  mapStateToProps,
  mapDispatchToProps
)(MyComponent)