3 回答
TA贡献1780条经验 获得超4个赞
要以编程方式添加属性,您可以使用 JSX 扩展语法{...props}:
class MyComponent extends React.Component {
state = {
error: false,
helperText: '',
additionalProps: {}
}
validateEmail = event => {
if (event.target.name !== "email") {
///Set HelperText and error props here
this.setState({
additionalProps: {error: true, helperText: "some error here"}
})
}
};
render (
<TextField
{...this.state.additionalProps}
name="password"
variant="outlined"
label="Password"
type="password"
className={classNames(styles.signUpInputField, styles.override)}
onChange={this.handleChange}
onBlur={this.validate}
error={this.state.error}
/>
{this.state.helperText}
);
}
TA贡献1786条经验 获得超11个赞
您必须添加 usingstate而不是props。
class YourComponent extends React.Component {
constructor(){
state = {
error: false,
helperText: '',
}
}
validateEmail = event => {
if (event.target.name !== "email") {
///Set HelperText and error state here
this.setState({error: true, helperText: "some error here"})
}
};
render (
...
<TextField
name="password"
variant="outlined"
label="Password"
type="password"
className={classNames(styles.signUpInputField, styles.override)}
onChange={this.handleChange}
onBlur={this.validate}
error={this.state.error} // add this new line
/>
<span>{this.state.helperText}</span>
....
);
}
TA贡献1847条经验 获得超11个赞
您必须为组件添加一个状态。
对于多个输入,我实现了 这个
class MyComponent extends React.Component {
state = {
error: false,
helperText: '',
}
validateEmail = event => {
if (event.target.name !== "email") {
///Set HelperText and error props here
this.setState({error: true, helperText: "some error here"})
}
};
render (
<TextField
name="password"
variant="outlined"
label="Password"
type="password"
className={classNames(styles.signUpInputField, styles.override)}
onChange={this.handleChange}
onBlur={this.validate}
error={this.state.error}
/>
{this.state.helperText}
);
}
添加回答
举报