英文:
Cannot read properties of undefined (reading 'value')...TypeError: Cannot read properties of undefined (reading 'value')
问题
我正在尝试从一个Redux文件中访问相机图像到React文件。此代码来自"https://www.papareact.com/"。我对Redux很陌生,所以卡在这里。
这是cameraSlice.js Redux文件,我在其中捕获并存储图像。
import { createSlice } from '@reduxjs/toolkit';
export const cameraSlice = createSlice({
name: 'camera',
initialState: {
cameraImage: null,
},
reducers: {
setCameraImage: (state, action) => {
state.cameraImage += action.payload;
},
resetCameraImage: (state) => {
state.cameraImage = null;
}
},
});
export const { setCameraImage, resetCameraImage } = cameraSlice.actions;
export const selectCameraImage= (state) => state.cameraImage.value;
export default cameraSlice.reducer;
这是webcamCapture.js,其中捕获的图像被派发到另一个URL(预览)。
import React, { useCallback, useRef, useState } from 'react';
import Webcam from 'react-webcam';
import RadioButtonUncheckedIcon from '@mui/icons-material/RadioButtonUnchecked';
import { useDispatch } from 'react-redux';
import { setCameraImage } from './features/cameraSlice';
import { useNavigate } from 'react-router-dom';
import './WebcamCapture.css';
const videoConstraints = {
width: 250,
height: 400,
facingMode: "user",
};
function WebcamCapture() {
const webcamRef = useRef(null);
const dispatch = useDispatch();
const navigate = useNavigate();
const capture = useCallback(() => {
const imageSrc = webcamRef.current.getScreenshot();
dispatch(setCameraImage(imageSrc));
navigate('/preview');
}, [webcamRef])
return (
<div className='webcamCapture'>
<Webcam
audio={false}
height={videoConstraints.height}
ref={webcamRef}
screenshotFormat='image/jpeg'
width={videoConstraints.width}
videoConstraints={videoConstraints}
/>
<RadioButtonUncheckedIcon
className='webcamCapture_button'
onClick={capture}
fontSize='large'
/>
</div>
)
}
export default WebcamCapture;
最后,在Preview.js中,我正在访问cameraSlice.js中的selectCameraImage函数。
import React from 'react';
import './Preview.css';
import { useSelector } from 'react-redux';
import { selectCameraImage } from './features/cameraSlice';
function Preview() {
const cameraImage = useSelector(selectCameraImage);
return (
<div className='preview'>
<h2>This is your preview</h2>
<img src={cameraImage} alt='' />
</div>
);
}
export default Preview;
但是一旦我点击相机按钮,它就会重定向到预览页面,但会出现错误“Cannot read properties of undefined (reading 'value')”。请帮忙。
英文:
I'm trying to access the camera image from one redux file to react file .This code is from "https://www.papareact.com/" .I'm new to redux and stuck here.
This is the cameraSlice.js redux file where I'm capturing the image and storing it .
import { createSlice } from '@reduxjs/toolkit';
export const cameraSlice = createSlice({
name: 'camera',
initialState: {
cameraImage:null,
},
// The `reducers` field lets us define reducers and generate associated actions
reducers: {
setCameraImage: (state, action) => {
state.cameraImage += action.payload;
},
resetCameraImage: (state) => {
state.cameraImage =null;
}
},
});
export const { setCameraImage, resetCameraImage } = cameraSlice.actions;
// The function below is called a selector and allows us to select a value from
// the state. Selectors can also be defined inline where they're used instead of
// in the slice file. For example: `useSelector((state: RootState) => state.counter.value)`
export const selectCameraImage= (state) => state.cameraImage.value;
export default cameraSlice.reducer;
This is the webcamCapture.js where it the image that got captured is dispatched to another url (preview)
import React, { useCallback, useRef, useState } from 'react';
import Webcam from 'react-webcam';
import RadioButtonUncheckedIcon from '@mui/icons-material/RadioButtonUnchecked';
import { useDispatch } from 'react-redux';
import { setCameraImage } from './features/cameraSlice';
import { useNavigate } from 'react-router-dom';
import './WebcamCapture.css';
const videoConstraints = {
width: 250,
height: 400,
facingMode:"user",
};
function WebcamCapture() {
const webcamRef=useRef(null);
const dispatch = useDispatch();
// BEM naming
const navigate = useNavigate() //const history=useHistory(); useHistory is now useNavigate in v6
//using hook
const capture = useCallback(()=>{
const imageSrc = webcamRef.current.getScreenshot(); //generates base64 image
dispatch(setCameraImage(imageSrc));
navigate('/preview');
}, [webcamRef])
return (
<div className='webcamCapture'>
<Webcam
audio={false}
height={videoConstraints.height}
ref={webcamRef}
screenshotFormat='image/jpeg'
width={videoConstraints.height}
videoConstraints={videoConstraints} //object
/>
<RadioButtonUncheckedIcon
className='webcamCapture_button'
onClick={capture}
fontSize='large'
/>
</div>
)
}
export default WebcamCapture;
and at the end I'm accessing the cameraSlice.js function selectCameraImage in Preview.js
import React from 'react';
import './Preview.css';
import { useSelector } from 'react-redux';
import { selectCameraImage } from './features/cameraSlice';
function Preview() {
const cameraImage= useSelector(selectCameraImage); //pulls image from redux
return (
<div className='preview'>
<h2> This is your preview</h2>
<img src={cameraImage} alt='' />
</div>
);
}
export default Preview;
but as soon as I click on the camera button it redirects to preview page yet gives the error
Cannot read properties of undefined (reading 'value')
TypeError: Cannot read properties of undefined (reading 'value').... Please help.
I was capturing image using react-webcam . I was supposed to display the image captured in the next navigation (Preview) navigation.
答案1
得分: 0
这是错误的 => 在您的切片文件中
export const selectCameraImage= (state) => state.cameraImage.value;
正确的将是 =>
export const selectCameraImage= (state) => state.camera.cameraImage;
英文:
This is Wrong => in your Slice File
export const selectCameraImage= (state) => state.cameraImage.value;
Correct Will Be
=>
export const selectCameraImage= (state) => state.camera.cameraImage;
答案2
得分: 0
- 我会假设你的存储设置类似于:
const reducer = combineReducers({
camera: cameraSlice.reducer,
...
})
const store = createStore(reducer)
你的 "slice" 是 camera
,你的 "reducer" setCameraImage
会改变该 "slice" 状态内的 cameraImage
的值。
setCameraImage
会将有效负载连接到state.cameraImage
本身(看起来像一个字符串),位于camera
"slice" 内,而你的选择器尝试访问.value
。
将这两者结合起来,看起来你需要更改你的选择器,类似于以下内容:
export const selectCameraImage = (state) => state.camera.cameraImage;
你还需要确保你使用有效负载设置相机图像,而不是与初始值 null
连接它会有问题 - 例如:
setCameraImage: (state, action) => {
state.cameraImage = action.payload;
}
然后,你还需要确保你的选择器能够在 "Preview" 组件中从 Redux 中接收值,例如通过将你的组件在更高级别包装在 React-Redux 提供程序中,参考:https://react-redux.js.org/api/provider#basic-usage
英文:
- I'll assume your store is setup something like:
const reducer = combineReducers({
camera: cameraSlice.reducer,
...
})
const store = createStore(reducer)
Your slice is camera
and your reducer setCameraImage
changes the value of cameraImage
within that slice state.
setCameraImage
concatenates the payload ontostate.cameraImage
itself (looks like a string) inside thecamera
slice, whereas your selector attempts to access.value
.
Combining the two, it appears you need to change your selector to something like the following:
export const selectCameraImage = (state) => state.camera.cameraImage;
You will also want to make sure you are setting the camera image with the payload as concatenating it with your initial value null
will be problematic - something like:
setCameraImage: (state, action) => {
state.cameraImage = action.payload;
},
Then you will also want to ensure that your selector can receive the value from redux in the Preview component e.g. by wrapping your component at a higher level with the React-Redux provider https://react-redux.js.org/api/provider#basic-usage
答案3
得分: 0
问题: 应该从状态中访问 cameraImage 属性,而不是 state.cameraImage.value
更新 "cameraSlice.js" 如下:
export const selectCameraImage = (state) => state.camera.cameraImage;
此外,您需要在 "cameraSlice.js" 文件中进行以下更改。
setCameraImage: (state, action) => {
state.cameraImage = action.payload;
},
=====================编辑=======================
您可以在 "Preview" 组件中编辑 capture 变量值,只需尝试在 useEffect 中添加以下依赖。
const capture = useCallback(() => {
const imageSrc = webcamRef.current.getScreenshot(); // 生成 base64 图像
dispatch(setCameraImage(imageSrc));
navigate('/preview');
}, [webcamRef, dispatch, navigate]);
然后,在后面 - 控制台输出 cameraImage。这样,您可以了解它仍然是未定义的,还是正在获取值。
const cameraImage = useSelector(selectCameraImage);
console.log(cameraImage);
英文:
Problem: It should be accessing the cameraImage property from the state, not state.cameraImage.value
Update the "cameraSlice.js" as this:
export const selectCameraImage = (state) => state.camera.cameraImage;
Also, you need to make below changes in "cameraSlice.js" file.
setCameraImage: (state, action) => {
state.cameraImage = action.payload;
},
=====================Edit=======================
You can edit the capture variable value in the "Preview" Component, just try to add below dependency in useeffect.
const capture = useCallback(() => {
const imageSrc = webcamRef.current.getScreenshot(); // generates base64 image
dispatch(setCameraImage(imageSrc));
navigate('/preview');
}, [webcamRef, dispatch, navigate]);
And, later on - Console the cameraImage. So, you can get to know it's still undefined or getting values.
const cameraImage = useSelector(selectCameraImage);
console.log(cameraImage);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论