Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use Zepto inside ReactJS components? #1243

Closed
r3verser opened this issue Nov 26, 2016 · 5 comments
Closed

How to use Zepto inside ReactJS components? #1243

r3verser opened this issue Nov 26, 2016 · 5 comments

Comments

@r3verser
Copy link

When importing, or requiring got error like "Cannot read property 'createElement'" .

@r3verser
Copy link
Author

Anyone?:confused:

@r14c
Copy link

r14c commented Dec 17, 2016

i'm using webpack and babel. installed zepto using npm.

import $ from 'zepto'
// some component

has this result

Uncaught TypeError: Cannot read property 'createElement' of undefined
    at eval (zepto.js:23)
    at eval (zepto.js:939)
    at Object.eval (zepto.js:4)
    at emptyArray (zepto.js:4)
    at eval (zepto.js:7)
    at Object.1057 (8.8.js:179)
    at __webpack_require__ (main.js:583)
    at fn (main.js:107)
    at eval (Checkbox.vue?./~/babel-loader!./~/vue-loader/lib/selector.js?type=script&index=0:7)
    at Object.1056 (8.8.js:169)

@madrobby madrobby closed this as completed Aug 6, 2017
@KroniK907
Copy link

I'm still having this issue... Is there a better way to import zepto?

@albizures
Copy link

Hi, an easy workaround, if you are using webpack, it is to use imports-loader like this:

rules : [
     {
       test: require.resolve('zepto'),
       use: 'imports-loader?this=>window',
    }
]

source: https://webpack.js.org/loaders/imports-loader/#webpack-config-js

@iagobruno
Copy link

Zepto is not optimized to work with react on the server side, it needs to access the "window" and "document", but you can use it in places you are sure will only be called on the client side:

componentDidMount() {
  const $ = import('zepto')
    
  $('#root').find('#header').hasClass('show')
}

You can also use on dynamic components.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants