React-Redux 组件未在商店中显示新道具
React-Redux Component not showing new props in store
我正在尝试使用 React 创建我自己的警报组件(在本例中,也是 Bootstrap v4)。基本上,如果发生需要通知用户的事情,请创建一条消息,将其放入商店并让 React 呈现警报。我知道我正在做的事情应该是可能的,但是我是新手,可以做出反应,我 missing/don 不明白反应是如何工作的,这导致没有显示任何警报。
首先,我提醒所有其他组件可用,所以我把它放在我的 app.js
:
import React from 'react';
import ReactDOM from 'react-dom';
import { Provider } from 'react-redux';
import { PersistGate } from 'redux-persist/integration/react';
import AppRouter from './routers/AppRouter';
import configureStore from './store/configureStore';
import Alerts from './components/controls/Alerts';
const { store, persistor } = configureStore();
const jsx = (
<Provider store={store}>
<PersistGate loading={null} persistor={persistor}>
<Alerts />
<AppRouter />
</PersistGate>
</Provider>
);
ReactDOM.render(jsx, document.getElementById('root'));
接下来是 Alerts
的组件。首先是动作:
// DISPLAY_ALERT
export const displayAlert = (message, severity) => ({
type: 'DISPLAY_ALERT',
message: message,
severity: severity
});
// DISMISS_ALERT
export const dismissAlert = (id) => ({
type: 'DISMISS_ALERT',
id: id
});
减速器:
const alertsDefaultState = [];
const alertNotify = (state, action) => {
let queue = state;
if (!queue || !Array.isArray(queue))
queue = [];
let newAlert = {
id: getUniqueId(),
message: action.message,
severity: action.severity
};
queue.push(newAlert);
return queue;
};
const alertDismiss = (state, action) => {
const newQueue = state.filter((element) => element.id !== action.id);
return newQueue;
};
const getUniqueId = () => {
return (Date.now().toString(36) + Math.random().toString(36).substr(2, 5)).toUpperCase();
};
export default (state = alertsDefaultState, action) => {
switch (action.type) {
case 'DISPLAY_ALERT':
return alertNotify(state, action);
case 'DISMISS_ALERT':
return alertDismiss(state, action);
case 'LOG_OUT_OF_API':
return [];
default:
return state;
}
};
店铺:
import { createStore, combineReducers } from 'redux';
import { persistStore, persistReducer } from 'redux-persist';
import storage from 'redux-persist/lib/storage';
import alertsReducer from '../reducers/alerts';
export default () => {
const persistConfig = {
key: 'root',
storage,
};
let reducers = combineReducers({
// Other reducers
alerts: alertsReducer
});
let store = createStore(
persistReducer(persistConfig, reducers),
window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__()
);
let persistor = persistStore(store);
return { store, persistor };
};
最后 Alerts
个组件:
import React from 'react';
import { connect } from 'react-redux';
import { dismissAlert } from '../../actions/alerts';
class Alerts extends React.Component {
constructor(props) {
super(props);
}
getAlerts = () => {
if (!this.props.alerts || this.props.alerts.length === 0)
return null;
const alertFixed = {
position:'fixed',
top: '0px',
left: '0px',
width: '100%',
zIndex: 9999,
borderRadius: '0px'
};
return (
<div style={alertFixed}>
{
this.props.alerts.map((alert) => {
const alertClass = `alert alert-${alert.severity} alert-dismissible m-4`
setTimeout(() => {
this.props.dispatch(dismissAlert(alert.id));
}, 5000);
return (
<div key={alert.id} id={alert.id} className={alertClass} role="alert">
<button type="button" className="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
{ alert.message }
</div>
);
}
)
}
</div>
);
}
render() {
return this.getAlerts()
}
}
const mapStateToProps = (state) => {
return {
alerts: state.alerts
}
};
export default connect(mapStateToProps)(Alerts);
最后一件事,我有一个 const class 用于警报类型:
export default {
Info: 'info',
Success: 'success',
Warning: 'warning',
Error: 'danger',
};
如果我 运行 上面的代码并且在 alerts store
中有一些东西,那么它将被渲染。但是,如果我在事件中向商店添加某些内容,例如单击按钮,我可以看到警报被添加到商店,但组件不会将其添加到 DOM.
我错过了什么?
编辑:
数组是 Javascript 中的引用类型
在你的
const alertNotify = (state, action) => {
let queue = state;
if (!queue || !Array.isArray(queue))
queue = [];
let newAlert = {
id: getUniqueId(),
message: action.message,
severity: action.severity
};
queue.push(newAlert);
return queue;
};
而不是做这样的事情
let queue = state;
您需要制作一份 它的副本(而不是引用它),然后
queue.push(newAlert);
即将您的初始队列声明更改为此(我正在使用传播运算符复制您通过的状态,然后将 newAlert 推送到您的队列中
let queue = [...state];
由于您在返回时排队,其中没有状态
这个条件被触发了
if (!this.props.alerts || this.props.alerts.length === 0)
我正在尝试使用 React 创建我自己的警报组件(在本例中,也是 Bootstrap v4)。基本上,如果发生需要通知用户的事情,请创建一条消息,将其放入商店并让 React 呈现警报。我知道我正在做的事情应该是可能的,但是我是新手,可以做出反应,我 missing/don 不明白反应是如何工作的,这导致没有显示任何警报。
首先,我提醒所有其他组件可用,所以我把它放在我的 app.js
:
import React from 'react';
import ReactDOM from 'react-dom';
import { Provider } from 'react-redux';
import { PersistGate } from 'redux-persist/integration/react';
import AppRouter from './routers/AppRouter';
import configureStore from './store/configureStore';
import Alerts from './components/controls/Alerts';
const { store, persistor } = configureStore();
const jsx = (
<Provider store={store}>
<PersistGate loading={null} persistor={persistor}>
<Alerts />
<AppRouter />
</PersistGate>
</Provider>
);
ReactDOM.render(jsx, document.getElementById('root'));
接下来是 Alerts
的组件。首先是动作:
// DISPLAY_ALERT
export const displayAlert = (message, severity) => ({
type: 'DISPLAY_ALERT',
message: message,
severity: severity
});
// DISMISS_ALERT
export const dismissAlert = (id) => ({
type: 'DISMISS_ALERT',
id: id
});
减速器:
const alertsDefaultState = [];
const alertNotify = (state, action) => {
let queue = state;
if (!queue || !Array.isArray(queue))
queue = [];
let newAlert = {
id: getUniqueId(),
message: action.message,
severity: action.severity
};
queue.push(newAlert);
return queue;
};
const alertDismiss = (state, action) => {
const newQueue = state.filter((element) => element.id !== action.id);
return newQueue;
};
const getUniqueId = () => {
return (Date.now().toString(36) + Math.random().toString(36).substr(2, 5)).toUpperCase();
};
export default (state = alertsDefaultState, action) => {
switch (action.type) {
case 'DISPLAY_ALERT':
return alertNotify(state, action);
case 'DISMISS_ALERT':
return alertDismiss(state, action);
case 'LOG_OUT_OF_API':
return [];
default:
return state;
}
};
店铺:
import { createStore, combineReducers } from 'redux';
import { persistStore, persistReducer } from 'redux-persist';
import storage from 'redux-persist/lib/storage';
import alertsReducer from '../reducers/alerts';
export default () => {
const persistConfig = {
key: 'root',
storage,
};
let reducers = combineReducers({
// Other reducers
alerts: alertsReducer
});
let store = createStore(
persistReducer(persistConfig, reducers),
window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__()
);
let persistor = persistStore(store);
return { store, persistor };
};
最后 Alerts
个组件:
import React from 'react';
import { connect } from 'react-redux';
import { dismissAlert } from '../../actions/alerts';
class Alerts extends React.Component {
constructor(props) {
super(props);
}
getAlerts = () => {
if (!this.props.alerts || this.props.alerts.length === 0)
return null;
const alertFixed = {
position:'fixed',
top: '0px',
left: '0px',
width: '100%',
zIndex: 9999,
borderRadius: '0px'
};
return (
<div style={alertFixed}>
{
this.props.alerts.map((alert) => {
const alertClass = `alert alert-${alert.severity} alert-dismissible m-4`
setTimeout(() => {
this.props.dispatch(dismissAlert(alert.id));
}, 5000);
return (
<div key={alert.id} id={alert.id} className={alertClass} role="alert">
<button type="button" className="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
{ alert.message }
</div>
);
}
)
}
</div>
);
}
render() {
return this.getAlerts()
}
}
const mapStateToProps = (state) => {
return {
alerts: state.alerts
}
};
export default connect(mapStateToProps)(Alerts);
最后一件事,我有一个 const class 用于警报类型:
export default {
Info: 'info',
Success: 'success',
Warning: 'warning',
Error: 'danger',
};
如果我 运行 上面的代码并且在 alerts store
中有一些东西,那么它将被渲染。但是,如果我在事件中向商店添加某些内容,例如单击按钮,我可以看到警报被添加到商店,但组件不会将其添加到 DOM.
我错过了什么?
编辑:
数组是 Javascript 中的引用类型
在你的
const alertNotify = (state, action) => {
let queue = state;
if (!queue || !Array.isArray(queue))
queue = [];
let newAlert = {
id: getUniqueId(),
message: action.message,
severity: action.severity
};
queue.push(newAlert);
return queue;
};
而不是做这样的事情
let queue = state;
您需要制作一份 它的副本(而不是引用它),然后
queue.push(newAlert);
即将您的初始队列声明更改为此(我正在使用传播运算符复制您通过的状态,然后将 newAlert 推送到您的队列中
let queue = [...state];
由于您在返回时排队,其中没有状态
这个条件被触发了
if (!this.props.alerts || this.props.alerts.length === 0)