使用不带React的Redux导出功能

时间:2018-09-24 22:33:52

标签: javascript redux

我希望能够在不使用任何React的情况下创建连接到Redux的功能列表,以便可以在我的应用程序的各个组件中使用这些功能。不使用React / react组件怎么办?我的方法请参见以下内容。我可以使用react-redux的connect吗?谢谢。

import PropTypes from 'prop-types';
import {connect} from 'react-redux';
import {fetchUsers} from './actions';

class Sample {
  getUsers() {
    this.props.fetchUsers();
  }

  displayUser() {
    if (this.props.isSignedIn) {
      return (
        <h1>`The user ID is: ${this.props.userId}`</h1>
      );
    }
  }
}

Sample.propTypes = {
  isSignedIn: PropTypes.bool.isRequired,
  userId: PropTypes.number.isRequired,
  fetchUsers: PropTypes.func.isRequired,
};

const mapStateToProps = (state) => ({
  isSignedIn: state.isSignedIn,
  userId: state.userId,
});

const mapDispatchToProps = (dispatch) => ({
  fetchUsers: () => {
    dispatch(fetchUsers());
  },
});

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

您也可以在此处查看我的index.js

import React from 'react';
import {Provider} from 'react-redux';
import {ConnectedRouter} from 'react-router-redux';
import createHistory from 'history/createBrowserHistory';
import App from './app';

const history = createHistory();
const store = configureStore(history);

render(
  <Provider store={store}>
    <ConnectedRouter history={history}>
      <App />
    </ConnectedRouter>
  </Provider>, document.getElementById('app'),
);

0 个答案:

没有答案