F
F
fif2022-04-01 15:25:34
React
fif, 2022-04-01 15:25:34

How to add a class to an element from another component?

Here is the first file:

<div className="form__item">
    <NameInput refNameInput={this.nameInput} />
</div>


Here is the second file:
export default class NameInput extends React.Component<MyType> {
  constructor(props: MyType) {
    super(props);
  }

  render() {
    return (
      <>
        <label htmlFor="formName" className="form__label">
          Name:
        </label>
        <input
          ref={this.props.refNameInput}
          minLength={4}
          type="text"
          id="formName"
          name="name"
          className="form__input"
        />
      </>
    );
  }
}


How can I add an error class from 1 file to input from 2 files?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
low molecular macro, 2022-04-01
@molekulyarniy

The parent is the first file, I import the second in 1

depending on what you mean by "pass the class".
If you want to switch the class on an element in the child component from the parent, then pass a boolean variable as prop to the child element, for example. Depending on this value, add/ remove class error
<div className={isError ? 'error' : '')}>
<div style={errorClassStyles}>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question