react-logo/frontend/src/components/myform.js

42 lines
909 B
JavaScript

import React from 'react'
import { connect } from 'react-redux'
import { addProduct } from '../actions/products'
class MyForm extends React.Component {
constructor(props) {
super(props);
this.state = {name: ''};
this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}
handleChange(evt) {
this.setState({ name: evt.target.value });
}
handleSubmit(evt) {
console.log(`Votre nom est ${this.state.name}`);
evt.preventDefault();
}
componentDidMount() {
this.props.dispatch(addProduct('pomme', 10));
}
render() {
return (
<form onSubmit={this.handleSubmit}>
<label>
Nom:
<input type="text" value={this.state.name} onChange={this.handleChange} />
</label>
<input type="submit" value="Soumettre" />
</form>
);
}
}
export default connect()(MyForm)