B
B
brainexplosion2018-07-01 19:22:17
React
brainexplosion, 2018-07-01 19:22:17

How can I make the user's last name appear in the first paragraph when typing, the first name in the second, and the middle name in the third?

Help me solve a simple task on React'e. Given an input and 3 paragraphs. The user's full name is entered into the input, separated by a space. Make sure that when typing, the user's last name appears in the first paragraph, the first name in the second, and the middle name in the third.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2018-07-01
@brainexplosion

class App extends React.Component {
  state = {
    value: '',
  }

  onChange = ({ target: { value } }) => {
    this.setState({ value });
  }

  render() {
    const name = this.state.value.split(/\s+/);

    return (
      <div>
        <input onChange={this.onChange} />
        <p>Ф: {name[0]}</p>
        <p>И: {name[1]}</p>
        <p>О: {name[2]}</p>
        {name[3] && <p>WTF??! {name.slice(3)}</p>}
      </div>
    );
  }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question