meteor / react-packages

Meteor packages for a great React developer experience
http://guide.meteor.com/react.html
Other
571 stars 157 forks source link

allow use external React #323

Closed crapthings closed 3 years ago

crapthings commented 3 years ago

image

Error: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:

  1. You might have mismatching versions of React and the renderer (such as React DOM)
  2. You might be breaking the Rules of Hooks
  3. You might have more than one copy of React in the same app See https://fb.me/react-invalid-hook-call for tips about how to debug and fix this problem.

we're moving react to use cdn version, but react-meteor-data doesn't use global one

CaptainN commented 3 years ago

You'd probably want to create a stub react module, that would simply re-export the global React instance. Internally, all react-meteor-data does is import from the 'react' node_module. You can probably work this out on your own, without any changes to the internals of react-meteor-data.

You could use Meteor's "nodeModules" key in package.json to make sure your local package gets picked up for recompilation, and/or use "babel-plugin-transform-imports" to redirect the imports to your local package.

crapthings commented 3 years ago

it looks nodeModules in package.json doesn't work with babel plugin

https://github.com/meteor/meteor/issues/11332

CaptainN commented 3 years ago

It definitely works. I use it all the time. Here is an example:

"babel": {
    "plugins": [
      "@babel/plugin-proposal-optional-chaining",
      "@babel/plugin-proposal-do-expressions",
      [
        "transform-imports",
        {
          "cloudinary": {
            "transform": "cloudinary/lib/cloudinary.js"
          },
          "@material-ui/core": {
            "transform": "@material-ui/core/${member}",
            "preventFullImport": true
          },
          "@material-ui/icons": {
            "transform": "@material-ui/icons/${member}",
            "preventFullImport": true
          },
          "@material-ui/styles": {
            "transform": "@material-ui/styles/${member}",
            "preventFullImport": true
          },
          "uniforms": {
            "transform": "uniforms/src/${member}",
            "preventFullImport": true
          },
          "uniforms-material": {
            "transform": "uniforms-material/src/${member}",
            "preventFullImport": true
          }
        }
      ],
      "lodash",
      "npdev-react-loadable-babel",
      [
        "@babel/plugin-transform-react-jsx",
        {
          "pragma": "h"
        }
      ]
    ]
  },
  "meteor": {
    "mainModule": {
      "client": "client/main.js",
      "server": "server/main.js"
    },
    "nodeModules": {
      "recompile": {
        "cloudinary": true,
        "simpl-schema": "legacy",
        "uniforms": true,
        "uniforms-material": true
      }
    }
  }

Note: You have to use the recompile setting above for it to work.

crapthings commented 3 years ago

for now we use a stub react module just

package.json

{
  "name": "react",
  "version": "17.0.1",
  "main": "index.js"
}

index.js

modules.exports = global.React;

so it doesn't touch ast