I'm reading a React-Redux tutorial. However, since all examples are made of class-type components, I am trying to change this to a functional component. But it doesn't work. What am I doing wrong?
this is the original version
import React from "react";
import { connect } from "react-redux";
import { addTodo } from "../redux/actions";
class AddTodo extends React.Component {
constructor(props) {
super(props);
this.state = { input: "" };
}
updateInput = input => {
this.setState({ input });
};
handleAddTodo = () => {
this.props.addTodo(this.state.input);
this.setState({ input: "" });
};
render() {
return (
<div>
<input
onChange={e => this.updateInput(e.target.value)}
value={this.state.input}
/>
<button className="add-todo" onClick={this.handleAddTodo}>
Add Todo
</button>
</div>
);
}
}
export default connect(null, { addTodo })(AddTodo);
// export default AddTodo;
And this is my code
import React, { useState } from "react";
import { connect } from "react-redux";
import { addTodo } from "../redux/actions";
function AddTodo() {
const initialState = {
input: "",
};
const [inputValue, setInput] = useState(initialState);
const updateInput = (input) => {
setInput({ input });
};
const handleAddTodo = () => {
addTodo(inputValue);
setInput({ input: "" });
};
return (
<div>
<input onChange={(e) => updateInput(e.target.value)} value={input} />
<button className="add-todo" onClick={handleAddTodo}>
Add Todo
</button>
</div>
);
}
export default connect(null, { addTodo })(AddTodo);
// export default AddTodo;
The error message is : TypeError (0 , _react.useState) is not a function
and this is the original code enter link description here