I am developing a TODO app using react redux typescript. I am unable to dispatch an action. I am new to redux so stuck in it. I am creating an event onclick handleSubmitButton then it is throwing dispatch error. Can someone help me in that ?
AddToDo.tsx
const AddToDo: React.FC = () => {
const [inputValue, setInputValue] = useState<string>("");
const handleSubmitButton = (e: React.MouseEvent<HTMLButtonElement>)=> {
e.preventDefault()
console.log(inputValue, "inputValue");
dispatch(addTodo(inputValue))
}
return (
<div>
<form noValidate>
<div className="form-group">
<label htmlFor="todoitem">Todo Item:</label>
<input type="text" className="form-control" placeholder="Enter Text" value={inputValue} id="todoitem" onChange={handleEditTodoItem} />
</div>
<button type="submit" className="btn btn-primary" onClick={handleSubmitButton}>Submit</button>
</form>
</div>
)
}
export default connect()(AddToDo)
Below is my action code -
export const addTodo = (name: string): AddTodoAction => (
{
type: ActionTypes.ADD_TODO,
payload: {
todo: {
id: nextId++,
name: name,
done: false
}
}
}
)
and Now my reducer code -
export function reducer(state: State = initialState, action: Action) {
switch (action.type) {
case ActionTypes.ADD_TODO: {
const todo = action.payload.todo
return {
...state,
todos: [...state.todos, todo]
}
}
}
}