Search code examples
javascriptreactjsrecoiljs

TypeError: Object(...) is not a function ReactJS + Recoil


I am using ReactJs and Recoil. When exporting an atom and importing it into App.js, I get a TypeError: Object (...) is not a function, what is the problem?


atoms.js:

    import atom from 'recoil';

    export const textState = atom({
    key: 'textState', // unique ID (with respect to other atoms/selectors)
    default: '', // default value (aka initial value)
  });

App.js:

import {
  useRecoilState, atom
} from 'recoil';
import { textState } from "./atoms"

Solution

  • Looks like you might need to destruct atom from the recoil library

    Try changing your import to look like this:

    import { atom } from 'recoil'

    More information in their docs: https://recoiljs.org/docs/introduction/getting-started

    Learn more about destructuring in JavaScript: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment