Learning basics of react here.
I have a simple Zipcode form I'm trying to update the Zipcode in the form component I made and in the app parent component I made.
In my onSumbit prop I call a function to update and added
e.preventDefault();
but the submit button still refreshes everything and i'm pretty sure loses my data.
I commented out my onInput functions and props because I realize it's not affecting my onSubmit action but let me know if that's needed in React for some reason.
I can add more if needed. In my index I am using bootstrap4 cdn
App.js:
import React, { Component } from 'react';
import GMap from './GMap';
import ZipCode from './ZipCode';
import './css/App.css';
class App extends Component {
constructor(props){
super(props);
this.state = {zipcode : ''};
this.onZipCodeChange = this.onZipCodeChange.bind(this);
}
onZipCodeChange(e){
e.preventDefault();
console.log('App has detected ZipCode Change');
const value = e.target;
this.setState({zipcode: value});
}
render() {
return (
<div className="App">
<header>
<nav className="navbar navbar-expand-lg navbar-light bg-light">
<a className="navbar-brand" href="#">Navbar</a>
<button className="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span className="navbar-toggler-icon"></span>
</button>
<div className="collapse navbar-collapse" id="navbarNav">
<ul className="navbar-nav">
<li className="nav-item active">
<a className="nav-link" href="#">Home <span className="sr-only">(current)</span></a>
</li>
<li className="nav-item">
<a className="nav-link" href="#">Features</a>
</li>
<li className="nav-item">
<a className="nav-link" href="#">Pricing</a>
</li>
<li className="nav-item">
<a className="nav-link disabled" href="#">Disabled</a>
</li>
</ul>
</div>
</nav>
</header>
<ZipCode onSubmit={this.onZipCodeChange} />
<GMap />
</div>
);
}
}
export default App;
ZipCode.js:
import React, { Component } from 'react';
import './css/App.css';
class ZipCode extends Component {
constructor(props) {
super(props);
this.state = {zipcode: ''};
// this.updateZip = this.updateZip.bind(this);
this.submitZipCode = this.submitZipCode.bind(this);
}
componentDidMount() {
console.log(" ZipCode will mount");
}
componentWillUnmount() {
console.log("ZipCode will unmount");
}
submitZipCode(e){
e.preventDefault();
const { value } = this.state;
this.setState({zipcode: value});
const { onSubmit } = this.props; //pull out to call method it is linked to
console.log('submitting zipcode');
onSubmit(value);
}
// updateZip(e){
// const value = e.target;
// this.setState({ zipcode : value });
// console.log('zipcode updated to: '+ this.state.zipcode.value);
// }
// onInput={this.updateZip}
render(){
return(
<div className="row">
<div className="col-sm-4 col-md-4 col-lg-4">
<form onSubmit={this.submitZipCode}>
<label>Zip Code</label>
<input
type="input"
name="zipcode"
value={this.zipcode}
/>
<button type="submit" className='btn btn-success'>Submit</button>
</form>
</div>
</div>
);
}
}
export default ZipCode;