简体   繁体   English

如何动态添加元素属性

[英]how to dynamically add element property

I am working on reactjs app and I am stuck on adding a property dynamically. 我正在使用reactjs应用程序,并且坚持动态添加属性。

I have email input field and a submit button and when a submit button is clicked I want to add error message if email is not valid. 我有电子邮件输入字段和一个提交按钮,当单击一个提交按钮时,如果电子邮件无效,我想添加错误消息。 Here is what I have done so far... 到目前为止,这是我所做的...

<TextField label='Email'/>
<Button onPress()=>this.doSomethingFunction() />

I want to doSomethingfunction() to dynamically add error property as follows 我想做doSomethingfunction()以动态添加错误属性,如下所示

 <TextField label='Email' error="invalid entry"/>

the problem I am facing is if I leave error property, as shown above, the error message appears even before user enters email address. 我面临的问题是,如果我离开error属性,如上所示,即使用户输入电子邮件地址,该错误消息也会出现。 The solution I am thinking of is to add the property after finding out if it is not valid but I dont how to do it dynamically. 我正在考虑的解决方案是在发现属性无效后添加属性,但是我不动态地进行操作。

You can use state variable to update UI. 您可以使用状态变量来更新UI。 so please try to do as following: 因此,请尝试执行以下操作:

constructor(props) {
  super(props);
  this.state = {
    validStatus: 0 // initial state, 1: valid state, 2: invalid state
  } 
}

doSomethingFunction = () => {
  ...
  if (email is valid) {
    this.setState({ validStatus: 1 });
  } else {
    this.setState({ validStatus: 2 });
  }
  ...
}

render() {
  const { validStatus } = this.state;
  return (
    <View>
      ...
      {validStatus !== 2 && <TextField label='Email'/>
        <Button onPress()=>this.doSomethingFunction() />}
      {validStatus === 2 && <TextField label='Email' error="invalid entry"/>}
      ...
    </View
  );
}

Just use state. 只是使用状态。 You can set the TextField error property to receive emailError state. 您可以将TextField错误属性设置为接收emailError状态。 So when you click at button, the emailError value will be changed. 因此,当您单击按钮时, emailError值将被更改。

class MyComponent extends React.Component {
  state = {
    emailError: ''
  }

  validate = () => {
    this.setState({emailError: 'invalid entry'})
  }

  render () {
    return (
      <View>
        <TextField label='Email' error={this.state.emailError} />
        <Button onPress={this.validate}/>
      </View>
    )
  }
}

This can be done using the state of component and doing some conditional rendering based on state. 这可以使用组件的状态并根据状态进行一些条件渲染来完成。 Here is a rough pseudo code which shows how you can use state to do conditional rendering 这是一个粗略的伪代码,它显示了如何使用状态进行条件渲染

class MyComponent extends React.Component {

  constructor() {
    super();
    this.state = {isError: false}
  }

  validate() {
    if (inputIsInvalid) {
      this.setState({isError: false})
    }
  }

  render() {
    let textField = <TextField label='Email'/>;
    if(this.state.isError)
      textField = <TextField label='Email' error="invalid entry"/>;
    return (
      {textField}
      <Button onPress()=>this.validate() />
    );
  }

}

I've never used react, so I could be far off base, but this documentation indicates that error is a boolean -- so if it's set at all, it will probably be interpreted as "true". 我从没使用过react,所以我可能还差得远,但是此文档表明error是布尔值-因此,如果将其设置为一点,则可能会将其解释为“ true”。

You can change the attributes on a standard HTML element with myElement.setAttribute("myAttribute", "myValue") . 您可以使用myElement.setAttribute("myAttribute", "myValue")更改标准HTML元素上的属性。 If this works for react components (without confusing react), then you could just set the error attribute when you want it to be true and remove it when you don't. 如果此方法适用于react组件(不会混淆react),那么您可以仅在要设置为true时设置error属性,否则将其删除。 (If react doesn't like for you to do this, then hopefully the above link can get you on the right track.) (如果react不希望您这样做,那么希望以上链接可以使您走上正确的轨道。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM