0

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

2 Answers 2

1

You initial state is an object (why?), so need to fix your input element:

<input onChange={(e) => updateInput(e.target.value)} value={inputValue.input} />

And your addTodo action is connected to the component's dispatch, so Redux injects it through props, while you using the action itself without a dispatch:

const handleAddTodo = () => {
  props.addTodo(inputValue);
  setInput({ input: "" });
};

Final version should look like so:

function AddTodo(props) {
  const [inputValue, setInput] = useState('');

  const handleAddTodo = () => {
    props.addTodo(inputValue);
    setInput('');
  };

  return (
    <div>
      <input onChange={(e) => setInput(e.target.value)} value={inputValue} />
      <button className="add-todo" onClick={handleAddTodo}>
        Add Todo
      </button>
    </div>
  );
}

export default connect(null, { addTodo })(AddTodo);

And if you already refactoring to function components, you should use Redux hooks and/with Redux-toolkit instead.

Sign up to request clarification or add additional context in comments.

Comments

1

Replace your functional component code with the following code. It should work. You used state update syntax in hooks that's why you are facing issues.

import React, { useState } from "react";
import { connect } from "react-redux";
import { addTodo } from "../redux/actions";

function AddTodo(props) {
  
  const [inputValue, setInput] = useState("");

  const updateInput = (input) => {
    setInput(input);
  };

  const handleAddTodo = () => {
    props.addTodo(inputValue);
    setInput("");
  };

  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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.