Etesam913 / react-magic-motion

react-magic-motion is a react.js library that ✨ magically animates your components.
https://react-magic-motion.com
MIT License
1.12k stars 14 forks source link

Error: Invalid tag: [object Object] #3

Open andromidasj opened 1 year ago

andromidasj commented 1 year ago

Hey! I have an app where I installed react-magic-motion, but am coming across this error and can't get it to work.

My app is built with:

I installed the package with bun a react-magic-motion, then wrapped the contents in my main page in <MagicMotion>, imported with import { MagicMotion } from "react-magic-motion";. I immediately get the following error:

index.js:654 Uncaught Error: Invalid tag: [object Object]
    at startChunkForTag ({MY_APP}/node_modules/react-dom/cjs/react-dom-server.browser.development.js:2804:13)
    at pushStartGenericElement ({MY_APP}/node_modules/react-dom/cjs/react-dom-server.browser.development.js:2630:15)
    at pushStartInstance ({MY_APP}/node_modules/react-dom/cjs/react-dom-server.browser.development.js:2908:18)
    at renderHostElement ({MY_APP}/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5636:18)
    at renderElement ({MY_APP}/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5952:5)
    at renderNodeDestructiveImpl ({MY_APP}/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6104:11)
    at renderNodeDestructive ({MY_APP}/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6076:14)
    at renderNode ({MY_APP}/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6259:12)
    at renderChildrenArray ({MY_APP}/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6211:7)
    at renderNodeDestructiveImpl ({MY_APP}/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6141:7)
    at renderNodeDestructive ({MY_APP}/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6076:14)
    at renderContextProvider ({MY_APP}/node_modules/react-dom/cjs/react-dom-server.browser.development.js:5920:3)
    at renderElement ({MY_APP}/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6017:11)
    at renderNodeDestructiveImpl ({MY_APP}/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6104:11)
    at renderNodeDestructive ({MY_APP}/node_modules/react-dom/cjs/react-dom-server.browser.development.js:6076:14)
websocket.js:50 [HMR] connected

Some potentially notable package versions I'm running:

    "next": "^13.5.6",
    "react": "^18.2.0",
    "react-dom": "^18.2.0",
    "react-magic-motion": "^1.0.7",
Etesam913 commented 1 year ago

Yeah, so whenever you get Error: Invalid tag: [object Object] that means that there is a problem with one of the children of the MagicMotion tag. You can try adding key="exclude" to each potential child that you suspect to be causing the problem. (note: doing this will stop the child from being animated though).

It would be nice if I could see some of the code. That would be very helpful.

AndresdoSantos commented 1 year ago

Hello, I had the same error and my code is this

import { useLocalStorage } from 'usehooks-ts'
import { Sun, Moon } from '@phosphor-icons/react'
import { zinc } from 'tailwindcss/colors'
import { MagicMotion } from 'react-magic-motion'

function App() {
  const [theme, setTheme] = useLocalStorage('theme', 'light')

  useEffect(() => {
    document.body.classList.remove('light', 'dark')
    document.body.classList.add(theme)
  }, [theme])

  return (
    <MagicMotion>
      <div className="h-screen w-screen bg-white dark:bg-zinc-800">
        <div className="mx-auto sm:max-w-[800px] pt-10">
          <header className="flex items-center justify-between">
            <section className="flex items-center justify-center space-x-5">
              <h1 className="text-sm tracking-widest font-bold text-zinc-800 dark:text-white">
                HOJE
              </h1>

              <form>
                <input
                  type="text"
                  className="text-xs font-medium text-white px-2 h-8 w-64 rounded bg-zinc-200/50 dark:bg-zinc-700/50 border border-zinc-300 dark:border-zinc-600 focus:border-zinc-400 focus:dark:border-zinc-500 focus:outline-none"
                  placeholder="O que você quer fazer hoje?"
                />
              </form>
            </section>

            <button
              onClick={() => setTheme(theme === 'light' ? 'dark' : 'light')}
            >
              {theme === 'dark' ? (
                <Sun size={20} weight="duotone" color={zinc[50]} />
              ) : (
                <Moon size={20} weight="duotone" color={zinc[900]} />
              )}
            </button>
          </header>
        </div>
      </div>
    </MagicMotion>
  )
}

export default App
Etesam913 commented 1 year ago

So in your example, the problematic component was the components from @phosphor-icons/react. The error can be fixed by add key="exclude to both the <Sun> and <Moon> components.

Adding key="exclude" prevents the specified tags from being auto-animated by the <MagicMotion> tag.

A working example is found here: https://codesandbox.io/s/recursing-bell-yxdr6z?file=/src/App.tsx

There isn't anything really being animated here though as there are no layout shifts or changes from conditional rendering, so it probably doesn't make much sense to add the tag here.

Snarloff commented 1 year ago

InvalidCharacterError: Failed to execute 'createElement' on 'Document': The tag name provided ('[object Object]') is not a valid name.

Using next.js!

Etesam913 commented 1 year ago

Post your code so I can see what's going on

Snarloff commented 1 year ago

Post your code so I can see what's going on

import { Button } from '@/components/ui/button'
import { LuLayoutList } from 'react-icons/lu'
import { MagicMotion } from "react-magic-motion";

export function Hero() {
  return (
    <MagicMotion>
      <div className="flex max-w-xl flex-col gap-7">
        <h1 className="mt-4 text-5xl font-bold leading-none md:mt-0 md:text-6xl">
          Text here
        </h1>

        <h4 className="text-muted-foreground max-w-md text-start">
         Text Here
        </h4>
        <div className="flex w-full flex-col gap-2 md:flex-row md:gap-4">
          <Button variant="outline" className="border-lightGray rounded-3xl font-medium md:w-44">
            Text here
          </Button>
          <Button variant="outline" className="border-lightGray rounded-3xl font-medium text-white md:w-56 ">
            <LuLayoutList size={19} className="mr-2" />

          </Button>
        </div>
      </div>
    </MagicMotion>
  )
}

I've tried for tests

Etesam913 commented 1 year ago

Add key="exclude" to the LuLayoutList tag

ex:

 <LuLayoutList key="exclude" size={19} className="mr-2" />

This tells the library to not animate this svg

Etesam913 commented 1 year ago

I'll try to see if I can add some automatic detection of these errors and throw a nicer error message to the user.

Snarloff commented 1 year ago
 <LuLayoutList key="exclude" size={19} className="mr-2" />

The same error :/

Maestroabel commented 1 year ago

I have the same problem. I'm using Formik and I need an animation for the form, but Formik won't let me use it. Any recommendation?

Etesam913 commented 1 year ago

It would be nice if you could post a reproducible example in a code sandbox or something like that.

cesarochoa2006 commented 1 year ago

I´m having the same problem. Probably don't support Server side rendering ?

quangphantally commented 1 year ago

found out that any tags that are not html tags would throw that error, so wrap your component in a div and add key="exclude" to that component, or fix this lib :)

for ex:

this won't work

<MagicMotion>
  <div>
  {arr => arr.map(x => <Button>...</Button>) 
  </div>
</MagicMotion>

this will work

<MagicMotion>
  <div>
  {arr => arr.map(x => <div><Button key="exclude">...</Button></div>) 
  </div>
</MagicMotion>
zemariagp commented 1 year ago

experiencing the same. Only seems to happen on forwardRef components. Posted a PR to get the ball rolling

Etesam913 commented 1 year ago

Thanks to @zemariagp many instances of this error should be fixed in version 1.1.1 of react-magic-motion

See release notes here: https://github.com/Etesam913/react-magic-motion/releases/tag/v1.1.1

zemariagp commented 1 year ago

I'd be curious to know for which (if any) cases the fix on v1.1.1 is not sufficient. Anyone?