【发布时间】:2023-09-16 14:45:01
【问题描述】:
我正在尝试构建一个 redux-saga 程序。流程是当我单击测试按钮时,文本“单击按钮隐藏”将消失。我不知道接下来要处理什么以及如何处理。请帮我。提前致谢! 这是我不完整的代码:
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;
messages.js
import { defineMessages } from 'react-intl';
export const scope = 'app.containers.TestPage';
export default defineMessages({
header: {
id: `${scope}.header`,
defaultMessage: 'Click button to hide',
},
});
可加载的.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)
【问题讨论】:
标签: redux-saga react-boilerplate