emsiiggy
emsiiggy

Reputation: 343

React Component changing from function to class - State and state hook

I've got a question, because i can't find answer anywhere. The problem is: what is the initial value of the following hook: "Function" Example:

function Example() {
  const [settings, setSettings] = useState({});

  return (
     ...
  );
}

And here i'm writing this function "Example" as class, but i don't know how to initialize state (is it empty string or empty list or 0 value?)

class Example extends React.Component {
    constructor(props) {
        super(props);

        this.state = {
             settings: <-- and what here?
        };
    }

Upvotes: 0

Views: 32

Answers (1)

xyhhx
xyhhx

Reputation: 6664

useState is passed the initial value as its only param - in your case {}.

You can accomplish the same in your second code snippet like so:

class Example extends React.Component {
    constructor(props) {
        super(props);

        this.state = {
             settings: {}
        };
    }
}

Further reading:

Upvotes: 1

Related Questions