What I want to do is when I type some text in an input field, it should appear in another place realtime.
Below is my input;
To be short, in React, there's no two-way data-binding.
So when you want to implement that feature, try define a state
, and write like this, listening events, update the state, and React renders for you:
class NameForm extends React.Component {
constructor(props) {
super(props);
this.state = {value: ''};
this.handleChange = this.handleChange.bind(this);
}
handleChange(event) {
this.setState({value: event.target.value});
}
render() {
return (
);
}
}
Details here https://facebook.github.io/react/docs/forms.html
UPDATE 2020
Note:
LinkedStateMixin is deprecated as of React v15. The recommendation is to explicitly set the value and change handler, instead of using LinkedStateMixin.
above update from React official site . Use below code if you are running under v15 of React else don't.
There are actually people wanting to write with two-way binding, but React does not work in that way. If you do want to write like that, you have to use an addon for React, like this:
var WithLink = React.createClass({
mixins: [LinkedStateMixin],
getInitialState: function() {
return {message: 'Hello!'};
},
render: function() {
return ;
}
});
Details here https://facebook.github.io/react/docs/two-way-binding-helpers.html
For refs
, it's just a solution that allow developers to reach the DOM in methods of a component, see here https://facebook.github.io/react/docs/refs-and-the-dom.html