snippetjavascriptreactModerate
How to get the value of an input field using ReactJS?
Viewed 0 times
getfieldthevaluereactjsinputusinghow
Problem
I have the following React component:
The console is giving me
export default class MyComponent extends React.Component {
onSubmit(e) {
e.preventDefault();
var title = this.title;
console.log(title);
}
render(){
return (
...
...
this.title = c} name="title" />
...
...
Save
...
);
}
};The console is giving me
undefined - any ideas what's wrong with this code?Solution
You should use constructor under the class MyComponent extends React.Component
Then you will get the result of title
constructor(props){
super(props);
this.onSubmit = this.onSubmit.bind(this);
}Then you will get the result of title
Code Snippets
constructor(props){
super(props);
this.onSubmit = this.onSubmit.bind(this);
}Context
Stack Overflow Q#36683770, score: 24
Revisions (0)
No revisions yet.