I am new to React. What I want to do is have a submit button to log the data/state onto the console from the form's input fields which is in another component. To implement this, I have the ResultList component which renders the Filter component which has the form. When the submit button is clicked the data from the input fields should log onto the console. I used react-form-hook to implement the form. This is the link to the sandbox
ResultList component looks like:
import React, { Component } from 'react';
import { render } from 'react-dom';
import Filter from './Filter';
import { Button, Dropdown, Input, } from 'semantic-ui-react';
export default class ResultList extends Component {
constructor(props){
super(props);
this.state = {
myName: '',
myEmail: ''
};
}
handleParentData = (formModel) => {
console.log(formModel);
this.setState({...formModel},()=>alert(JSON.stringify(this.state)));
}
render() {
return (
<div>
<Filter handleData={this.handleParentData}/>
<p>{this.state.myName}</p>
<p>{this.state.email}</p>
</div>
)
}
}
and Filter component looks like:
import React, { Component } from 'react';
import {useForm} from 'react-hook-form';
export default function Filter() {
const {register,handleSubmit} = useForm();
const onSubmit = (data) => {
// console.log(data);
this.props.handleData(data);
}
return (
<div>
<form onSubmit={handleSubmit(onSubmit)}>
Name: <input type="text" name="myName" ref={register}/>
<br />
Email: <input type="text" name="myEmail" ref={register}/>
<br /><br />
<input type="submit" value="Submit"/>
</form>
</div>
)
}