我正在用reducer文件中的redux管理状态,该文件显示我正在设置新的状态值
reducer.js
import {motionSensorData} from "../sever/api";
import * as actionTypes from './actions';
const sensorData = [];
const sensorDataContext = [];
const initialState = {
sensorReadings: ["3pm"],
timeOfSensorReadings: [],
chartData:{}
};
const reducer = (state = initialState, action) => {
switch (action.type) {
case actionTypes.setSensorReadings:
motionSensorData((err, data) => {
console.log(data);
//Here I am adding the new data into the corresponding array
sensorData.push(data.numberOfBirds);
sensorDataContext.push(data.timeOfMeasurement);
console.log(sensorData);
});
console.log(sensorData)
return {
...state,
sensorReadings: sensorData,
timeOfSensorReadings: sensorDataContext
};
}
return state;
}
export default reducer;
这里发生的是每3秒数组sensorData和sensorDataContext变大->这就是控制台中的样子 sensorDataArray
但是,当console.log state.sensoReadings时,它是一个空数组[[]”,与sensorReadings的初始状态为下午3点相反。
我知道状态仅被更新一次,因为这也在控制台stateRecievingValues
中发生这些是应该在数组中的值。
如果您需要更多信息,或者我没有很好地解释问题,请提供任何帮助