babel-loader jsx SyntaxError: Unexpected token [duplicate]

Add “babel-preset-react” npm install babel-preset-react and add “presets” option to babel-loader in your webpack.config.js (or you can add it to your .babelrc or package.js: http://babeljs.io/docs/usage/babelrc/) Here is an example webpack.config.js: { test: /\.jsx?$/, // Match both .js and .jsx files exclude: /node_modules/, loader: “babel”, query: { presets:[‘react’] } } Recently Babel 6 was released and … Read more

Trace why a React component is re-rendering

If you want a short snippet without any external dependencies I find this useful componentDidUpdate(prevProps, prevState) { Object.entries(this.props).forEach(([key, val]) => prevProps[key] !== val && console.log(`Prop ‘${key}’ changed`) ); if (this.state) { Object.entries(this.state).forEach(([key, val]) => prevState[key] !== val && console.log(`State ‘${key}’ changed`) ); } } Here is a small hook I use to trace updates to … Read more

Pass props to parent component in React.js

Edit: see the end examples for ES6 updated examples. This answer simply handle the case of direct parent-child relationship. When parent and child have potentially a lot of intermediaries, check this answer. Other solutions are missing the point While they still work fine, other answers are missing something very important. Is there not a simple … Read more

where is create-react-app webpack config and files?

If you want to find webpack files and configurations go to your package.json file and look for scripts You will find that scripts object is using a library react-scripts Now go to node_modules and look for react-scripts folder react-script-in-node-modules This react-scripts/scripts and react-scripts/config folder contains all the webpack configurations.

How to add fonts to create-react-app based projects?

There are two options: Using Imports This is the suggested option. It ensures your fonts go through the build pipeline, get hashes during compilation so that browser caching works correctly, and that you get compilation errors if the files are missing. As described in “Adding Images, Fonts, and Files”, you need to have a CSS … Read more

When to use ES6 class based React components vs. functional ES6 React components?

New Answer: Much of the below was true, until the introduction of React Hooks. componentDidUpdate can be replicated with useEffect(fn), where fn is the function to run upon rerendering. componentDidMount methods can be replicated with useEffect(fn, []), where fn is the function to run upon rerendering, and [] is an array of objects for which … Read more

How do I reference a local image in React?

First of all wrap the src in {} Then if using Webpack; Instead of: <img src={“./logo.jpeg”} /> You may need to use require: <img src={require(‘./logo.jpeg’)} /> Another option would be to first import the image as such: import logo from ‘./logo.jpeg’; // with import or … const logo = require(‘./logo.jpeg); // with require then plug … Read more