admin管理员组文章数量:1400329
I'm using xstate & React to manage my state machine. I am passing data through the onChange handler in the App function. The console log shows that event.target.value is a string but when setResult is ran, event.data is undefined.
I am getting the following error when typing something in the input field:
Cannot read properties of undefined (reading 'data') TypeError: Cannot read properties of undefined (reading 'data') at prompt
Below is my code, any help is appreciated:
import { useMachine } from "@xstate/react";
import React from "react";
import { assign, createMachine } from "xstate";
const newMachine = createMachine(
{
id: "newMachine",
initial: "idle",
context: {
result: "",
prompt: "",
},
on: {
TYPE: {
actions: ["setResult"],
},
target: ".secondary",
},
states: {
idle: {},
secondary: {
entry: "testPrint",
},
},
},
{
actions: {
setResult: assign({
prompt: (context, event) => {
console.log("Event: ", event);
return event.data;
},
}),
testPrint: () => {
console.log("TEST PRINT");
},
},
}
);
export default function App() {
const [current, send] = useMachine(newMachine);
const { result, prompt } = current.context;
return (
<div>
Result: {result} <br />
State: {current.value} <br />
<input
type="text"
value={prompt}
onChange={(event) => {
console.log("Event onChange: ", event.target.value);
send({ type: "TYPE", data: event.target.value });
}}
/>
</div>
);
}
I'm using xstate & React to manage my state machine. I am passing data through the onChange handler in the App function. The console log shows that event.target.value is a string but when setResult is ran, event.data is undefined.
I am getting the following error when typing something in the input field:
Cannot read properties of undefined (reading 'data') TypeError: Cannot read properties of undefined (reading 'data') at prompt
Below is my code, any help is appreciated:
import { useMachine } from "@xstate/react";
import React from "react";
import { assign, createMachine } from "xstate";
const newMachine = createMachine(
{
id: "newMachine",
initial: "idle",
context: {
result: "",
prompt: "",
},
on: {
TYPE: {
actions: ["setResult"],
},
target: ".secondary",
},
states: {
idle: {},
secondary: {
entry: "testPrint",
},
},
},
{
actions: {
setResult: assign({
prompt: (context, event) => {
console.log("Event: ", event);
return event.data;
},
}),
testPrint: () => {
console.log("TEST PRINT");
},
},
}
);
export default function App() {
const [current, send] = useMachine(newMachine);
const { result, prompt } = current.context;
return (
<div>
Result: {result} <br />
State: {current.value} <br />
<input
type="text"
value={prompt}
onChange={(event) => {
console.log("Event onChange: ", event.target.value);
send({ type: "TYPE", data: event.target.value });
}}
/>
</div>
);
}
Share
Improve this question
asked Mar 24 at 4:32
It's Just Me02It's Just Me02
1
1 Answer
Reset to default 0Just doing some basic debugging I see that prompt
is passed 2 arguments, the first is an object and the second is undefined
as you have now seen in your code. The first object, however, has context
and event
properties. You should likely destructure these from the first argument.
const newMachine = createMachine(
{
id: "newMachine",
initial: "idle",
context: {
result: "",
prompt: "",
},
on: {
TYPE: {
actions: ["setResult"],
},
target: ".secondary",
},
states: {
idle: {},
secondary: {
entry: "testPrint",
},
},
},
{
actions: {
setResult: assign({
prompt: ({ context, event }) => { // <-- destructure context and event
// ...
return event.data;
},
}),
testPrint: () => {
// ...
},
},
}
);
本文标签: javascriptWhy is the data undefined if it39s passing dataStack Overflow
版权声明:本文标题:javascript - Why is the data undefined if it's passing data? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744261027a2597717.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论