我有react-redux应用程序,它是一个模态。我需要从外部切换它。我最初的应用程序看起来像这样:
import React, { PureComponent } from 'react';
import PropTypes from 'prop-types';
import { applyMiddleware, compose, createStore } from 'redux';
import { Provider } from 'react-redux';
import thunk from 'redux-thunk';
import App from './lib/containers/App';
import reducer from './lib/ducks/reducers';
import MY_PROPTYPES from './lib/proptypes';
/* eslint-disable no-underscore-dangle */
const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__
|| compose;
/* eslint-enable */
const middleware = [];
middleware.push(thunk);
if (process.env.NODE_ENV !== 'production') {
// eslint-disable-next-line global-require
const { createLogger } = require('redux-logger');
middleware.push(createLogger());
}
export default class Gallery extends PureComponent {
render() {
const initState = {
...
};
const store = createStore(
reducer,
initState,
composeEnhancers(applyMiddleware(...middleware))
return (
<Provider store={store}>
<App
onClose={e => onClose(e)}
isVisible={isVisible}
initialPosition={initialPosition}
/>
</Provider>
);
}
}
当我关闭模态并打开它时,这导致了我的问题。尽管存储发生了更改,但它停止了重新渲染。因此,请相应地存储更改,但是我的应用程序对这些更改没有反应。 我发现这是因为我创建了商店的其他实例,然后再次将其传递给我的应用程序,但它仍连接到先前的商店。
因此,我更改了入口点: 我添加了状态为store的构造函数,并将创建存储更改为以下代码:
let newStore = store;
if (!newStore) {
newStore = createStore(
reducer,
initState,
composeEnhancers(applyMiddleware(...middleware))
);
this.setState({ store: newStore });
}
这个newStore我传递给我的提供者。这解决了我的问题,App会根据商店进行更改,但是现在我遇到了这个JS错误:
警告:在现有状态转换过程中无法更新(例如 在
render
中)。渲染方法应该是道具的纯粹功能 和状态。
我的package.json:
{
"name": "@fishingbooker/gallery",
"version": "1.0.0",
"description": "",
"main": "./dist",
"directories": {
"doc": "docs"
},
"scripts": {
"lib": "babel src -d dist",
"lib:watch": "babel src -w -d dist",
"docs": "webpack-dev-server --open",
"start": "webpack-dev-server --open --mode development --host 0.0.0.0 --disable-host-check --https",
"docs:prof": "webpack -p"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"@babel/cli": "^7.1.2",
"@babel/core": "^7.1.2",
"@babel/helper-plugin-utils": "^7.0.0",
"@babel/plugin-proposal-class-properties": "^7.2.3",
"@babel/preset-env": "^7.0.0",
"@babel/preset-react": "^7.0.0",
"@babel/register": "^7.0.0",
"babel-core": "^7.0.0-bridge.0",
"babel-loader": "^8.0.4",
"html-webpack-plugin": "^3.2.0",
"webpack": "^4.20.2",
"webpack-cli": "^3.1.2",
"webpack-dev-server": "^3.1.9"
},
"dependencies": {
"@fishingbooker/react-slick": "0.0.5",
"axios": "^0.18.0",
"classnames": "^2.2.6",
"lodash": "^4.17.11",
"mousetrap": "^1.6.2",
"prop-types": "^15.6.2",
"react": "^16.5.2",
"react-dom": "^16.5.2",
"react-redux": "^6.0.0",
"react-swipe": "^6.0.3",
"redux": "^4.0.1",
"redux-logger": "^3.0.6",
"redux-thunk": "^2.3.0"
}
}
有人知道我该如何解决该错误,或者如何重新呈现我的入口点,以便我的应用程序保持与商店的连接。
谢谢。