javascript - Passing getChildContext() to reactjs higher order components throws unexpected token error in webpack -
i got following example here make higher order component passing context reliably.
however, there unexpected token
error passing getchildcontext()
function when compiling script using webpack:
getchildcontext = () => getchildcontext(this.props); ^^^
code:
const providecontext = (childcontexttypes, getchildcontext) => (component) => { class contextprovider extends react.component { getchildcontext = () => getchildcontext(this.props); render() { return <component {...this.props} />; } } contextprovide.childcontexttypes = childcontexttypes; return contextprovider; };
is there workaround way of passing function?
my webpack config:
var common = { output: {path: build_dir}, module: { loaders: [ { test: /\.js$/, loader: 'babel', include: app_dir, query: { presets: ['es2015', 'react'] } }, { test: /\.css$/, loader: "style-loader!css-loader" }, { test: /\.(png|woff|woff2|eot|ttf|svg)$/, loader: 'url-loader?limit=100000' }, { test: /\.(png|jpg)$/, loader: 'file-loader' } ] }, resolve: { alias: { 'react': path.join(__dirname, 'node_modules', 'react'), 'react-dom': path.join(__dirname, 'node_modules', 'react-dom') }, extensions: ['', '.js'] }, }; config.push( merge(common, { entry: { itempage:app_dir+'/page/'+filename+'.js', }, devtool: 'source-map', output: { filename: "[name].js", librarytarget: 'umd', library: "[name]" }, externals: [{ react: { root: 'react', commonjs2: 'react', commonjs: 'react', amd: 'react', }, "react-dom": "reactdom" }] }) ); module.exports = config;
you getting unexpected token
because missing 1 babel plugin, transform-class-properties included in stage-1
preset
with stage-1, works
without stage-1, not work
so in order fix it, have to
1º install stage-1
npm install --save-dev babel-preset-stage-1
2º add .babelrc
{ "presets": ["react","es2015","stage-1"] }
or webpack config
query: { presets: ["react","es2015","stage-1"] }
Comments
Post a Comment