React Hooks (useState) and Mobx [No mobx-react-lite]

Amir-Mousavi picture Amir-Mousavi · Mar 25, 2019 · Viewed 13.2k times · Source

in my react application (with typescript) I want to use React hooks (specifically useState) to manage the form state and meanwhile use it as an observable component for Mobx store but I get the error

Hooks can only be called inside the body of a function component.

so for instance in the following component

import * as React from "react";
import { inject, observer } from "mobx-react";
import { MyStore } from "./MyStore";

interface IProps {
  myStore?: MyStore;
  id: string;
}

const MyComponent: React.FC<IProps> = props => {
  const [state, setState] = React.useState("");
  return (
    <div>
      <h1>{props.id}</h1>
    </div>
  );
};
export default inject("myStore")(observer(MyComponent));

I saw a solution but that was using React.createContext for exporting the store class. is not where the old approach for Mobx and Hooks?

here is the sanbox for the example

Answer

Amir-Mousavi picture Amir-Mousavi · Sep 27, 2019

Thanks to @Tholle mentioning the Mobx version, now that the Mobx 6 is released this question is solved