[英]Updating a React state variable throws cross-origin error
At the moment I am working on a project where I want to get data, more specifically a list/Array of Strings, from my Python API and display that list with React.目前我正在做一个项目,我想从我的 Python API 中获取数据,更具体地说是字符串列表/数组,并使用 React 显示该列表。 I am getting the list and it looks completely fine.
我得到了清单,它看起来完全没问题。 No
undefined
values, everything is a String
and it really is an Array
.没有
undefined
的值,一切都是String
并且它确实是Array
。 So the retrieved data appears to be valid .所以检索到的数据似乎是有效的。 After retrieving the data I want to set it as a state variable in React so that I can pass it to another component as a prop.
检索数据后,我想在 React 中将其设置为 state 变量,以便我可以将其作为道具传递给另一个组件。
I am using pywebview as a React GUI for my Python application.我正在使用pywebview作为我的 Python 应用程序的 React GUI。 The JavaScript modules are being bundled by webpack.
JavaScript 模块由 webpack 捆绑。
Whenever I call setCourses()
I am getting the following error and React shows a whitescreen.每当我调用
setCourses()
时,我都会收到以下错误,并且 React 会显示白屏。
Error: A cross-origin error was thrown.
错误:引发了跨域错误。 React doesn't have access to the actual error object in development.
React 无法访问开发中的实际错误 object。 See https://reactjs.org/docs/cross-origin-errors.html for more information.
有关详细信息,请参阅https://reactjs.org/docs/cross-origin-errors.html 。
setCourses(["foo0", foo1", "foo2"]);
this error is not thrown.setCourses(["foo0", foo1", "foo2"]);
时不会抛出此错误。setCourses(new Array(response));
setCourses(new Array(response));
it does not throw an error but somehow joins the Arrays elements to a new Array with the size of 1: ["foo0", "foo1", "foo2"] -> ["foo0,foo1,foo2"].cheap-module-source-map
setting in case one is using webpack.cheap-module-source-map
设置提到的 React 错误页面。 Setting this didn't help either.As you can see below, I am fetching the data from the Python API in a useEffect
hook and trying to set the state which shall rerender my component so that the child component <CourseList courses={courses} />
will display the courses. As you can see below, I am fetching the data from the Python API in a
useEffect
hook and trying to set the state which shall rerender my component so that the child component <CourseList courses={courses} />
will display the courses.
export default function Dashboard(props) { // some props etc. were removed to keep this snippet simple // the state variable which causes issues const [courses, setCourses] = React.useState([]); // get Data from Python API React.useEffect(()=>{ getData(); }, []); /** * Gets the needed data (login status, courses, username) by calling the Python API */ function getData() { window.pywebview.api.foo().then((foo) => { // do stuff }).then(() => { // fetch the courses from the Python API window.pywebview.api.getCourses().then((response) => { // try to update the state variable setCourses(response); // <------- throws the error }).catch((response) => {console.log(response); }); } return ( <Box> <MainAppBar /> <Grid container spacing={0}> <Grid item> <MenuList /> </Grid> <Grid item> <CourseList courses={courses} /> {/* passing the courses to the CourseList component */} </Grid> <FloatingSyncButton /> </Grid> </Box> ) } export function CourseList({ courses }) { // logic... return ( <List> {/* trying to list each course item */} {courses.map((courseName) => { return ( <ListItem key={courseName} disablePadding > <ListItemButton> <Checkbox/> <ListItemAvatar> <Avatar>{courseName}</Avatar> </ListItemAvatar> <ListItemText primary={courseName} /> </ListItemButton> </ListItem> ); })} </List> ); }
I would appreciate any help.我将不胜感激任何帮助。 Thank you!
谢谢!
Could you try this?你能试试这个吗?
const getData = async () => {
try {
const api = await window.pywebview.api.foo()
// do some stuff with api
// fetch the courses from the Python API
const courseList = await window.pywebview.api.getCourses()
setCourses(courseList);
}
catch (e) {
console.log(error)
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.