Preact with styled-components causes error in browser console

Viewed 1619

Simply importing styled-components leads to this error in the browser console:

styled-components.browser.esm.js?face:1670 Uncaught TypeError: (0 , _react.createContext) is not a function
    at Object.eval (styled-components.browser.esm.js?face:1670)
    at eval (styled-components.browser.esm.js:2490)
    at Object../node_modules/styled-components/dist/styled-components.browser.esm.js (vendors~index.js:167)
    at __webpack_require__ (index.js:79)
    at eval (index.js?12d5:2)
    at Object../src/index.js (index.js:165)
    at __webpack_require__ (index.js:79)
    at checkDeferredModules (index.js:46)
    at index.js:152
    at index.js:155

I'm using webpack, preact, Babel.

Code to reproduce is literally just this, in a new, empty build:

src/index.js:

import { h, Component, render } from "preact"
import styled from "styled-components"

package.json:

{
  "name": "App",
  "version": "0.0.1",
  "description": "Web app.",
  "main": "index.js",
  "scripts": {
    "build": "rm -rf dist/* && NODE_ENV=development webpack -d --config webpack.conf.js --env development"
  },
  "devDependencies": {
    "babel-core": "^6.26.3",
    "babel-loader": "^7.1.5",
    "babel-plugin-transform-react-jsx": "^6.24.1",
    "babel-preset-env": "^1.7.0",
    "babel-preset-react": "^6.24.1",
    "html-webpack-plugin": "^3.2.0",
    "preact": "^8.4.2",
    "preact-compat": "^3.18.4",
    "styled-components": "^4.1.3",
    "webpack": "^4.28.4",
    "webpack-cli": "^3.2.1"
  },
  "dependencies": {}
}

webpack.conf.js:

const path = require("path")
const webpack = require("webpack")
const htmlWebpackPlugin = require("html-webpack-plugin")

module.exports = {
  entry: {
    index: "./src/index.js"
  },
  module: {
    rules: [{
      test: /\.(js|jsx)$/i,
      use: {
        loader: 'babel-loader',
        options: {
          presets: [
            'babel-preset-env',
            'babel-preset-react'
          ],
          plugins: [
            [ 'babel-plugin-transform-react-jsx', {
              pragma: 'h'
            }],
          ]
        }
      }
    }]
  },
  output: {
    publicPath: ("http://your-hostname.com/"),
    path: path.resolve(__dirname, 'dist'),
    filename: '[name].js',
    chunkFilename: '[name].js'
  },
  optimization: {
    splitChunks: {
      chunks: 'all'
    },
    occurrenceOrder: true
  },
  plugins: [
    new htmlWebpackPlugin({
      template: './public/index.html',
      filename: 'index.html'
})
  ],
  resolve: {
    alias: {
      'react': 'preact-compat',
      'react-dom': 'preact-compat'
    }
  }
}

Build using npm run build works fine, but visiting the resulting page in the browser produces the above error.

Any pointers would be very gratefully received.

Edit:

According to this issue logged against Preact-CLI, preact does not have react's contextApi so I should downgrade styled-components to v3 instead of v4. Downgrading does indeed seem to resolve the issue.

Confusingly though, Jason Miller, author of preact, says in a tweet that "preact supports context just fine". I'm probably misunderstanding this.

2 Answers

Disclaimer: I work on preact.

You're seeing this error because the createContext-API is not supported in Preact 8.x. It will be part of our next major release which is right around the corner. We're currently in beta status and are hoping to move forward to a release candidate soon.

If you absolutely need styled-components to work with Preact 8.x, then your only option is to downgrade to styled-components V3 like @ukosteopath suggested.

Related