[英]Reactjs TypeError: Cannot read property 'id' of undefined
I facing problem " TypeError: Cannot read property 'id' of undefined " when i using Grid
in elements.我在元素中使用Grid
时遇到问题“ TypeError: Cannot read property 'id' of undefined ”。
the error i facing like this.我面临的错误是这样的。
Here is my sample code.这是我的示例代码。
In this code only i am facing the issue when im using
Grid
在此代码中,只有我在使用Grid
时遇到问题
<ThemeProvider theme={theme}>
<Card>
<Edit {...props}>
<SimpleForm>
<Grid container spacing={1} align="center">
<Grid item sm={6}>
<ReferenceInput
source="appId"
reference="_apps"
allowEmpty
defaultValue={
props.location.data ? props.location.data.appId : ""
}
validate={required()}
>
<SelectInput optionText="label" />
</ReferenceInput>
</Grid>
<Grid item sm={6}>
<SelectInput
source="iconColor"
choices={[
{ id: "primary", name: "primary" },
{ id: "secondary", name: "secondary" },
{ id: "action", name: "action" },
{ id: "disabled", name: "disabled" },
]}
/>
</Grid>
<Grid item sm={6}>
<ReferenceManyField
label={"resources._fields.name"}
reference="_fields"
target="eid"
>
<Datagrid>
<TextInput type="text" source="label" />
<TextInput type="text" source="component" />
<BooleanField source="showInList" />
<BooleanField source="showInFilter" />
<BooleanField source="showInCreate" />
<BooleanField source="showInEdit" />
<BooleanField source="showInShow" />
<EditButton />
</Datagrid>
</ReferenceManyField>
</Grid>
<Grid item sm={6}>
<ReferenceManyField
label={"resources._triggers.name"}
reference="_triggers"
target="eid"
>
<Datagrid>
<TextInput type="text" source="name" />
<BooleanField source="beforeInsert" />
<BooleanField source="afterInsert" />
<BooleanField source="beforeUpdate" />
<BooleanField source="afterUpdate" />
<BooleanField source="beforeDelete" />
<BooleanField source="afterDelete" />
<EditButton />
</Datagrid>
</ReferenceManyField>
</Grid>
<Grid item sm={6}>
<AddTriggerButton />
<AddFieldButton />
</Grid>
</Grid>
</SimpleForm>
</Edit>
</Card>
</ThemeProvider>
This is my actual code in that screenshot:这是我在该屏幕截图中的实际代码:
const TriggerButton = ({ children, to, ...props }) => {
const CustomLink = React.useMemo(
() =>
React.forwardRef((linkProps, ref) => (
<StyledLink ref={ref} to={to} {...linkProps} />
)),
[to]
);
return (
<Button {...props} component={CustomLink}>
{children}
</Button>
);
};
export default class AddTriggerButton extends React.Component {
constructor(props) {
super(props);
this.state = {
open: false,
props: props,
};
}
render() {
return (
<div>
<TriggerButton
component={Link}
to={{
pathname: "/_triggers/create",
data: { eid: this.state.props.record.id },
}}
startIcon={<AddIcon />}
>
New Trigger
</TriggerButton>
</div>
);
}
}
Because you don't pass any props to AddTriggerButton
so props is {}
.因为您没有将任何道具传递给AddTriggerButton
所以道具是{}
。 So this.state.props
is {}
and this.state.props.record
become undefined
.所以this.state.props
是{}
而this.state.props.record
变成undefined
。
This is not good logic.这不是好的逻辑。 You need to pass props and use this.props
instead this.state.props
您需要传递道具并使用this.props
代替this.state.props
<AddTriggerButton record={{id: value}} /> <AddTriggerButton record={{id: value}} />
Change this.state.props.record.id
to this.props.record.id
把this.state.props.record.id
this.props.record.id
Try to console.log(this.state.props.record.id) and shate here the reault.尝试 console.log(this.state.props.record.id) 并在此处屏蔽结果。 How you defined your the props?你如何定义你的道具? Maybe there is an asynchronous problem, so try to put '?'也许存在异步问题,所以尝试输入'?' before the id like this: this.state.props.record?.id在像这样的 id 之前:this.state.props.record?.id
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.