|
1 |
| -import React from 'react'; |
| 1 | +import React, { useState } from "react"; |
2 | 2 | import { connect } from "react-redux";
|
3 | 3 | import { addLabel } from "../redux/actions";
|
4 | 4 |
|
5 | 5 | const initialState = {
|
6 | 6 | name: "",
|
7 |
| - value: "" |
8 |
| -} |
9 |
| -class LabelsFilter extends React.Component { |
10 |
| - constructor(props) { |
11 |
| - super(props); |
12 |
| - this.state = initialState; |
13 |
| - } |
14 |
| - |
15 |
| - updateCurrentLabel = (name) => { |
16 |
| - this.setState({ name }); |
17 |
| - }; |
| 7 | + value: "", |
| 8 | +}; |
18 | 9 |
|
19 |
| - updateCurrentValue = (value) => { |
20 |
| - this.setState({ value }); |
| 10 | +function LabelsFilter() { |
| 11 | + const [state, setState] = useState(initialState); |
| 12 | + const updateCurrentLabel = (name) => { |
| 13 | + setState({ name }); |
21 | 14 | };
|
22 |
| - |
23 |
| - addLabel = (e) => { |
24 |
| - this.props.addLabel(this.state.name, this.state.value); |
25 |
| - this.setState(initialState); |
26 |
| - e.preventDefault(); |
| 15 | + const updateCurrentValue = (value) => { |
| 16 | + setState({ value }); |
27 | 17 | };
|
28 | 18 |
|
29 |
| - render() { |
30 |
| - return <form className="labels-new-label" onSubmit={this.addLabel}> |
| 19 | + return ( |
| 20 | + <form className="labels-new-label" onSubmit={addLabel}> |
31 | 21 | <input
|
32 | 22 | className="labels-new-input"
|
33 |
| - onChange={(e) => this.updateCurrentLabel(e.target.value)} |
| 23 | + onChange={(e) => updateCurrentLabel(e.target.value)} |
34 | 24 | placeholder="Name"
|
35 |
| - value={this.state.name} |
| 25 | + value={state.name} |
36 | 26 | />
|
37 | 27 | <input
|
38 | 28 | className="labels-new-input"
|
39 |
| - onChange={(e) => this.updateCurrentValue(e.target.value)} |
| 29 | + onChange={(e) => updateCurrentValue(e.target.value)} |
40 | 30 | placeholder="Value"
|
41 |
| - value={this.state.value} |
| 31 | + value={state.value} |
42 | 32 | />
|
43 |
| - <button className="btn labels-new-btn" onClick={this.addLabel}>Add</button> |
| 33 | + <button |
| 34 | + className="btn labels-new-btn" |
| 35 | + onClick={(e) => { |
| 36 | + addLabel(state.name, state.value); |
| 37 | + setState(initialState); |
| 38 | + e.preventDefault(); |
| 39 | + }} |
| 40 | + > |
| 41 | + Add |
| 42 | + </button> |
44 | 43 | </form>
|
45 |
| - } |
| 44 | + ); |
46 | 45 | }
|
47 | 46 |
|
48 |
| -export default connect( |
49 |
| - (x) => x, |
50 |
| - { addLabel } |
51 |
| -)(LabelsFilter); |
| 47 | +export default connect((x) => x, { addLabel })(LabelsFilter); |
0 commit comments