Показать / скрыть компонент, используя дескриптор события в redux-saga - PullRequest
0 голосов
/ 05 апреля 2019

Я пытаюсь создать программу «Редукс-сага».Суть в том, что когда я нажимаю кнопку «Тест», текст «Нажмите кнопку, чтобы скрыть» исчезает.Я не знаю, что делать дальше и как.Пожалуйста, помогите мне.Заранее спасибо!Вот мой неполный код:

constants.js

export const TEST_ACTION = 'app/TestPage/TEST_ACTION';

actions.js

export function defaultAction() {
  return {
    type: TEST_ACTION,
  };
}

reducer.js

export const initialState = fromJS({});
function testPageReducer(state = initialState, action) {
  switch (action.type) {
    case TEST_ACTION:
      return state;
    default:
      return state;
  }
}
export default testPageReducer;

сообщений.js

import { defineMessages } from 'react-intl';
export const scope = 'app.containers.TestPage';
export default defineMessages({
  header: {
    id: `${scope}.header`,
    defaultMessage: 'Click button to hide',
  },
});

Loadable.js

import loadable from 'loadable-components';
import LoadingIndicator from 'components/LoadingIndicator';
export default loadable(() => import('./index'), {
  LoadingComponent: LoadingIndicator,
});

saga.js

import { takeEvery } from 'redux-saga/effects';
import { TEST_ACTION } from './constants';

// Individual exports for testing
export function* testPageSaga() {
}
export default function* rootSaga() {
  yield takeEvery(TEST_ACTION, testPageSaga);
}

index.js

import React from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import { Helmet } from 'react-helmet';
import { FormattedMessage } from 'react-intl';
import { createStructuredSelector } from 'reselect';
import { compose } from 'redux';
import injectSaga from 'utils/injectSaga';
import injectReducer from 'utils/injectReducer';
import { Button } from 'antd';
import makeSelectTestPage from './selectors';
import reducer from './reducer';
import saga from './saga';
import messages from './messages';

export class TestPage extends React.PureComponent {
  render() {
    return (
      <div>
        <Helmet>
          <title>TestPage</title>
          <meta name="description" content="Description of TestPage" />
        </Helmet>
        <Button onClick={}>Test</Button>
        <FormattedMessage {...messages.header} />
      </div>
    );
  }
}
TestPage.propTypes = {
  dispatch: PropTypes.func.isRequired,
};
const mapStateToProps = createStructuredSelector({
  testPage: makeSelectTestPage(),
});
function mapDispatchToProps(dispatch) {
  return {
    dispatch,
  };
}
const withConnect = connect(
  mapStateToProps,
  mapDispatchToProps,
);
const withReducer = injectReducer({ key: 'testPage', reducer });
const withSaga = injectSaga({ key: 'testPage', saga });
export default compose(
  withReducer,
  withSaga,
  withConnect,
)(TestPage)

1 Ответ

1 голос
/ 05 апреля 2019

Вам не нужна сага в этом конкретном случае, вы можете просто импортировать свою константу в ваш редуктор. Вот полная реализация:

          // constants.js
          export const TOGGLE_BUTTON = 'App/TestPage/TOGGLE_BUTTON';

          // actions.js

          import { TOGGLE_BUTTON } from './constants';

          export const toggleButton = () => {
            return {
              type: 'TOGGLE_BUTTON',
            };
          }

          // reducer.js

          import { TOGGLE_BUTTON } from './constants';

          export const initialState = fromJS({
            showText: true,
          });

          function testPageReducer(state = initialState, action) {
            switch (action.type) {
              case TOGGLE_BUTTON:
                return state.update('showText', prevValue => !prevValue);
              default:
                return state;
            }
          }
          export default testPageReducer;


          // index.js

          import { toggleButton } from './actions';

          export class TestPage extends React.PureComponent {
            render() {
              const { onClick, testPage: { showText } } = this.props;

              return (
                <div>
                  <Helmet>
                    <title>TestPage</title>
                    <meta name="description" content="Description of TestPage" />
                  </Helmet>
                  <Button onClick={onClick}>Test</Button>
                  { showText && <FormattedMessage {...messages.header} />}
                </div>
              );
            }
          }

          TestPage.propTypes = {
            dispatch: PropTypes.func.isRequired,
            onClick: PropTypes.func.isRequired,
            testPage: PropTypes.shape({
              showText: PropTypes.bool,
            }),
          };

          const mapStateToProps = createStructuredSelector({
            testPage: makeSelectTestPage(),
          });

          function mapDispatchToProps(dispatch) {
            return {
              onClick: () => dispatch(toggleButton()),
            };
          }
          const withConnect = connect(
            mapStateToProps,
            mapDispatchToProps,
          );
          const withReducer = injectReducer({ key: 'testPage', reducer });
          const withSaga = injectSaga({ key: 'testPage', saga });
          export default compose(
            withReducer,
            withSaga,
            withConnect,
          )(TestPage)
...