gpt4 book ai didi

javascript - 变量替换解析错误(意外标记,预期 "...")

转载 作者:行者123 更新时间:2023-11-30 19:55:28 24 4
gpt4 key购买 nike

我有一些动态调整文本字段的代码。

此版本的代码工作正常:

  if (props.responsetxt === null) {
txtField = (
<TextField
autoFocus
margin="dense"
id="name"
label={emailField}
type="email"
fullWidth
onChange={e => emailFieldUpdate(e)}
/>
);

但是我正在使用 material-ui 并且我想利用他们的 error 选项 ( https://material-ui.com/demos/text-fields/ )

但是如果我修改我的代码如下:

let errorFlag = null;  // add this
txtField = (
<TextField
{errorFlag} // add this
autoFocus
margin="dense"
id="name"
label={emailField}
type="email"
fullWidth
onChange={e => emailFieldUpdate(e)}
/>
);

我得到一个错误:

Parsing error: Unexpected token, expected "..."

  Line 45:  Parsing error: Unexpected token, expected "..."

43 | txtField = (
44 | <TextField
> 45 | {errorFlag}
| ^
46 | autoFocus
47 | margin="dense"
48 | id="name"

我不明白为什么 labelonChange 动态参数可以正常工作,但 {errorFlag} 替换不能?

更新

function DownloadForm(props) {
const intl = props.intl;
const boxTitle = intl.formatMessage({ id: 'downloadRequest.title' });
const cancelButton = intl.formatMessage({ id: 'button.cancel' });
const closeButton = intl.formatMessage({ id: 'button.close' });
const downloadButton = intl.formatMessage({ id: 'button.download' });
const emailField = intl.formatMessage({ id: 'downloadRequest.emailField' });
let boxText = null;
let waitingAnimation = null;
let returnArr = {};
let errorFlag = null;
const emailFieldUpdate = e => {
returnArr['email'] = e.target.value;
if (!EmailValidator(e.target.value)) {
console.log('setting true !');
errorFlag=true;
}
};
returnArr['subset'] = props.selectedSubset;
if (props.showWaitingAnimation) {
waitingAnimation = <CircularProgress />;
}
if (props.responsetxt === null) {
returnArr['correlID'] = UUIDGenerator();
returnArr['boxOpened'] = TAI64.now().toHexString();
boxText = intl.formatMessage({ id: 'downloadRequest.prompt' });
} else {
boxText = props.responsetxt;
}
let txtField, submitButton, closeText;
if (props.responsetxt === null) {
txtField = (
<TextField
error={errorFlag}
autoFocus
margin="dense"
id="name"
label={emailField}
type="email"
fullWidth
onChange={e => emailFieldUpdate(e)}
/>
);
submitButton = (
<Button
color="primary"
onClick={() =>
props.submit(returnArr, process.env.REACT_APP_ITS_AWS_SQS_DOWNLOAD)
}
>
{downloadButton}
</Button>
);
closeText = cancelButton;
} else {
closeText = closeButton;
}
return (
<div>
<Dialog open={props.open} aria-labelledby="form-dialog-title">
<DialogTitle id="form-dialog-title">{boxTitle}</DialogTitle>
<DialogContent>
<DialogContentText>{boxText}</DialogContentText>
{waitingAnimation}
{txtField}
</DialogContent>
<DialogActions>
<Button color="primary" onClick={props.close}>
{closeText}
</Button>
{submitButton}
</DialogActions>
</Dialog>
</div>
);
}
export default injectIntl(DownloadForm);

最佳答案

来自文档:

  <TextField
error
id="standard-error"
label="Error"
defaultValue="Hello World"
className={classes.textField}
margin="normal"
/>

此处的errorerror={true} 的简短语法,无法动态重现。但是,您可以执行以下操作:

  <TextField
error={errorFlag}
autoFocus
margin="dense"
id="name"
label={emailField}
type="email"
fullWidth
onChange={e => emailFieldUpdate(e)}
/>

正如您的错误消息告诉您的那样,解构单个属性 JSON 也可能有效:

  <TextField
...{error : errorFlag}

将其重命名为 error 将进一步减少语法:

  <TextField
...{error}

编辑:

您正在使用无状态 React 组件,这意味着它永远不会自行重新呈现,调用 emailFieldUpdate 也不会。我将您的组件重构为有状态组件,其中 errorFlag 现在处于您的状态。

调用 this.setState({ errorFlag: true }) 将更新您的标志并重新呈现您的组件,向您显示错误。我还做了一些代码可读性更改:

import React, { Component } from 'react'
export default class DownloadForm extends Component {
constructor(props) {
super(props)

this.state = {
errorFlag: null,
returnArr: {}
}
}

emailFieldUpdate = e => {
this.setState({ returnArr:{ email: e.target.value }})
if (!EmailValidator(e.target.value)) {
console.log('setting true !');
this.setState({ errorFlag: true })
}
};

render() {
const { selectedSubset, responsetxt, showWaitingAnimation, intl, submit, open, close } = this.props //Decontructs your props
const { errorFlag, returnArr } = this.state //Deconstructs your state

const [boxTitle, cancelButton, closeButton, downloadButton, emailField] =
['downloadRequest.title', 'button.cancel', 'button.close', 'button.download', 'downloadRequest.emailField'].map(id =>
intl.formatMessage({ id })
);
let boxText = null;
let waitingAnimation = null;

returnArr['subset'] = selectedSubset;
if (showWaitingAnimation) {
waitingAnimation = <CircularProgress />;
}
if (!responsetxt) {
returnArr['correlID'] = UUIDGenerator();
returnArr['boxOpened'] = TAI64.now().toHexString();
boxText = intl.formatMessage({ id: 'downloadRequest.prompt' });
} else {
boxText = responsetxt;
}
return (
<div>
<Dialog open={open} aria-labelledby="form-dialog-title">
<DialogTitle id="form-dialog-title">{boxTitle}</DialogTitle>
<DialogContent>
<DialogContentText>{boxText}</DialogContentText>
{waitingAnimation}
{!responsetxt &&
<TextField
error={errorFlag}
autoFocus
margin="dense"
id="name"
label={emailField}
type="email"
fullWidth
onChange={this.emailFieldUpdate}
/>
}
</DialogContent>
<DialogActions>
<Button color="primary" onClick={close}>
{responsetxt ? closeButton : cancelButton}
</Button>
{!responsetxt &&
<Button
color="primary"
onClick={() => {submit(returnArr, process.env.REACT_APP_ITS_AWS_SQS_DOWNLOAD)}}>
{downloadButton}
</Button>
}
</DialogActions>
</Dialog>
</div>
);
}
}

您似乎在使用 React-intl,请查看此文档以将您的消息直接放入您的 JSX 中:https://github.com/yahoo/react-intl/wiki/Components#formattedmessage

我还建议阅读 conditional rendering in Reactdeconstruction in JS

关于javascript - 变量替换解析错误(意外标记,预期 "..."),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54046719/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com