我有一个项目,当状态通过WebSocket更改时,它会将值发送到服务器,我正在使用redux-TOOLKIT,我的状态如下
const initialState = {
exposure: false,
whiteBalance: false,
shutterSpeed: 1,
iso: 50,
temperature : 5500,
tint : 0,
brightness: { checked: false, value: 0 },
hue: { checked: false, value: 0 },
saturation: { checked: false, value: 0 },
vibrance: { checked: false, value: 0 },
contrast: { checked: false, value: 0 },
gamma: { checked: false, value: 0 },
sharpness: { checked: false, value: 0 },
};
const imageAdjustmentsSlice = createSlice({
name: "imageAdjustments",
initialState,
reducers: {
changeIso: (state, action: PayloadAction<number>): void => {
state.iso = action.payload;
},
changeTemperature: (state,action: PayloadAction<number>): void => {
state.temperature = action.payload;
},
changeTint: (state,action: PayloadAction<number>): void => {
state.tint = action.payload;
},
// and other reducers
}
});
const store = configureStore({
reducer: {
imgAdjustments:imageAdjustmentsSlice.reducer,
},
});
const WSController: React.FC = () => {
const videoStore = useAppSelector((state) => state.imgAdjustments);
const { sendMessage } = useWebSocket("ws://localhost:3000");
React.useEffect(() => {
// i want to send the changed value only not the whole state.
sendMessage(JSON.stringify(videoStore));
}, [videoStore ]);
return <></>
}
当videoStore更改时,我想将更改后的值发送到服务器.我想知道当状态改变时哪个值会改变. 例如,我使用dispatch在某处更改了iso值.我只想得到改变后的ISO值.就像这样:
const changedValue = getChangedValue(state) // returns { iso : 200 }
有没有函数/钩子或其他方法来实现这一点?或者我应该重新配置REDUX?