N
N
Nikolai Lapshin2016-10-13 01:48:04
React
Nikolai Lapshin, 2016-10-13 01:48:04

How to implement a simple example in react: fill in the input - its value is displayed in the text?

Don't throw slippers - I decided to pick react for the first time.
Basically hello world.
There is input, there is text on the page. We enter something into input - value is displayed in the text.
I, in fact, have a misunderstanding about how to implement this if the input is in one component, and the text is in another?
Or is it already crutches and bicycles and you can’t do that?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
Z
Zakhar Orlov, 2016-10-13
@divalign

Without redux:

const Input = (props) => (
  <input type="text" onChange={props.handleChange} />
)

const Output = (props) => (
  <div>{props.text}</div>
)

class App extends React.Component {
  constructor (props) {
    super(props)
    this.state = {}
    this.handleChange = this.handleChange.bind(this)
  }

  handleChange (e) {
    this.setState({
      value: e.target.value
    })
  }

  render () {
    return (
      <div>
        <Input handleChange={this.handleChange} />
        <Output text={this.state.value} />
      </div>
    )
  }
}

A
Alexander Taratin, 2016-10-13
@Taraflex

https://habrahabr.ru/post/279249/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question