React JS : Module Build Failuer, error showing in render colon - reactjs

I am getting error like this
Module build failed: SyntaxError
It gives me error in colon of render function
Here it is my code :
webpack.config.js
module.exports = {
entry : './main.js',
output : {
path : './',
filename : 'index.js'
},
devServer : {
inline : true,
port : 3333
},
module : {
loaders : [
{
test : /\.js$/,
exclude : /node_modules/,
loader : 'babel',
query :{
presets : ['es2015', 'react']
}
}
]
}
package.json
{
"name": "library",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "webpack-dev-server"
},
"author": "",
"license": "ISC",
"dependencies": {
"babel": "^6.5.2",
"babel-core": "^6.9.0",
"babel-preset-es2015": "^6.9.0",
"babel-preset-react": "^6.5.0",
"react": "^15.0.2",
"react-dom": "^15.0.2"
},
"devDependencies": {
"babel": "^6.5.2",
"babel-core": "^6.9.0",
"babel-loader": "^5.3.2"
}
main.js
import React from 'react';
import ReactDom from 'react-dom';
var Heading = React.createClass({
render : function() {
return <th>{this.props.heading}</th>;
}
});
var Headings = React.createClass({
render : function(){
var headings = this.props.headings.map(function(heading) {
return(<Heading heading = {name}/>);
});
return (<thead><tr>{headings}</tr><thead>);
}
});
var Row = React.createClass({
render : function() {
return (
<tr>
<td>{this.props.changeSet.when}</td>
<td>{this.props.changeSet.who}</td>
<td>{this.props.changeSet.description}</td>
</tr>
);
}
});
var Rows = React.createClass({
render : function() {
var rows = this.props.changeSets.map(function(changeSet) {
return(<Row changeSet = {changeSet} />);
});
return <tbody>{rows}</tbody>;
}
});
var App = React.createClass({
render : function() {
return (
<table className='table'>
<Headings headings={this.props.headings} />
<Rows changeSets={this.props.changeSets} />
</table>
);
}
});
/* Data Declaration */
var data = [
{
"when": "2 minutes ago",
"who": "Jill Dupre",
"description": "Created new account"
},
{
"when": "1 hour ago",
"who": "Lose White",
"description": "Added fist chapter"
},
{
"when": "2 hours ago",
"who": "Jordan Whash",
"description": "Created new account"
}
];
var headings = ['When','Who', 'Description'];
/* Call to Render Function for entire app */
ReactDom.render(<App headings={headings} changeSets={data} />, document.getElementById('container'));
Can somebody please provide a solution for it. I have included babel-core also but can't figure out what's wrong.

I tried setting up your code locally and was able to resolve the issue.
I was getting a Module build failed: ReferenceError: [BABEL] - path to file error
The reason is, The node API for babel has been moved to babel-core.So remove the babel dependency from both dependencies and devDependencies, move the babel-loader to your dependencies and clear the devDependencies of your package.json file and reinstall your modules. It should work
There are some syntax errors on
line 20 render : function(){
unclosed tags in line 15 return (<thead><tr>{headings}</tr><thead>);

Related

What loader I need for JSX compile?

Hi I am new doing webpackconfig for compile my react js component and I am triying to compile it for publish in npm(I am unsing react I started using jsc classes etc but always the same error, so I translate to js, but again the same error :/ I'm lost) :
my babelrc:
{
"presets": ["es2015", "react"]
}
my package.json:
{
"name": "react-chat-bot42",
"version": "0.6.7",
"private": false,
"description": "An ES6 npm module Chat Bot!",
"main": "dist/index.js",
"scripts": {
"build": "babel src -d dist",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "IƱigo Romero",
"license": "MIT",
"keywords": [
"Chabot",
"react"
],
"dependencies": {
"#babel/plugin-syntax-jsx": "^7.12.13",
"#babel/preset-react": "^7.12.13",
"babel-preset-es2015": "^6.24.1",
"extract-text-webpack-plugin": "^3.0.2",
"path": "^0.12.7",
"react": "^16.14.0",
"react-dom": "^17.0.1",
"webpack": "^4.29.6"
},
"devDependencies": {
"#babel/core": "^7.12.13",
"#babel/preset-env": "^7.12.13",
"autoprefixer": "^10.2.4",
"babel-cli": "^6.26.0",
"babel-core": "^7.0.0-bridge.0",
"babel-loader": "^8.2.2",
"babel-preset-env": "^1.7.0",
"babel-preset-es2015": "^6.24.1",
"babel-preset-stage-2": "^6.24.1",
"css-loader": "^5.0.2",
"html-webpack-plugin": "^5.0.0",
"webpack-cli": "^4.5.0"
},
"repository": {
"type": "git",
"url": "https://github.com/InigoRomero/npm-Chat-Bot"
}
}
my webpack.config.js:
const path = require('path')
module.exports = {
entry: path.resolve(__dirname, 'src', 'index.js'),
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'index.js'
}
}
The Error when I try to "webpack --mode=development" for compile:
ERROR in ./src/index.js 96:6
Module parse failed: Unexpected token (96:6)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
|
| return (
> <div id="container" className="container">
| <div id="chat" className="chat">
| <div id="messages" className="messages"></div>
My source code:
import React, {Component} from 'react';
import {Dprompts, DReplies, DnotFound} from './DefaultConstants'
//import './DefaultStyle.css'
const reactChatBot = props => {
let
prompts = props.Prompts ? props.Prompts : Dprompts,
Replies = props.Replies ? props.Replies : DReplies,
notFound = props.notFound ? props.notFound : DnotFound,
botIcon = props.botIcon,
userIcon = props.userIcon,
input = '',
text = '',
product = '';
window.onload = function() {
let inputField = document.getElementById("input");
inputField.addEventListener("keydown", (e) => {
if (e.code === "Enter") {
input = inputField.value;
inputField.value = "";
output();
}
});
}
function output() {
let product2;
let text2 = input.toLowerCase().replace(/[^\w\s]/gi, "").replace(/[\d]/gi, "").trim();
text2 = text2
.replace(/ a /g, " ")
.replace(/i feel /g, "")
.replace(/whats/g, "what is")
.replace(/please /g, "")
.replace(/ please/g, "")
.replace(/r u/g, "are you");
text = text2;
if (compare()) {
product2 = compare();
} else if (text2.match(/thank/gi)) {
product2 = "You're welcome!"
} else {
product2 = notFound[Math.floor(Math.random() * notFound.length)];
}
product = product2;
addChat();
}
function compare() {
let reply;
let replyFound = false;
for (let x = 0; x < prompts.length; x++) {
for (let y = 0; y < prompts[x].length; y++) {
if (prompts[x][y] === text) {
let replies = Replies[x];
reply = replies[Math.floor(Math.random() * replies.length)];
replyFound = true;
break;
}
}
if (replyFound) {
break;
}
}
return reply;
}
function addChat() {
const messagesContainer = document.getElementById("messages");
let userDiv = document.createElement("div");
userDiv.id = "user";
userDiv.className = "user response";
userDiv.innerHTML = `<img src="`+ userIcon +`" class="avatar"><span>${text}</span>`;
messagesContainer.appendChild(userDiv);
let botDiv = document.createElement("div");
let botImg = document.createElement("img");
let botText = document.createElement("span");
botDiv.id = "bot";
botImg.src = botIcon;
botImg.className = "avatar";
botDiv.className = "bot response";
botText.innerText = "...";
botDiv.appendChild(botText);
botDiv.appendChild(botImg);
messagesContainer.appendChild(botDiv);
messagesContainer.scrollTop = messagesContainer.scrollHeight - messagesContainer.clientHeight;
setTimeout(() => {
botText.innerText = `${product}`;
}, 2000
)
}
return (
<div id="container" className="container">
<div id="chat" className="chat">
<div id="messages" className="messages"></div>
<input id="input" type="text" placeholder="Say something..." autoComplete="off" autoFocus={true} />
</div>
</div>
);
};
export default reactChatBot;
and IF I try the webpack with this loader:
var path = require('path');
module.exports = {
entry: './src/index.js',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'index.js'
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader'
}
}
]
}
}
I got this error:
Error: Plugin/Preset files are not allowed to export objects, only functions. In C:\Users\SapoArmado\Desktop\npm-Chat-Bot\node_modules\babel-preset-es2015\lib\index.js
Thanks for your time!
Does your source file have a .js or .jsx extension? If it contains JSX syntax it should be .jsx or else compiler will complain

Running gunjs with Reactjs and webpack throws Reference Error in console

I am trying to install gun.js and run it inside a Reactjs webpack bundled app
var path = require('path'),
webpack = require('webpack');
module.exports = {
devtool: 'source-map',
target: 'node',
node: {
fs: 'empty'
},
entry: {
workboard: './src/workboard/main.js'
},
output: {
path: __dirname, filename: '/public/[name]/js/bundle.js'
},
module: {
loaders: [
{
test: /.js?$/,
loader: 'babel-loader',
exclude: /node_modules/,
query: {
presets: ['es2015', 'react', 'stage-2', 'stage-1']
}
}
],
noParse: [/aws-sdk/]
},
plugins: [
new webpack.DefinePlugin({ "global.GENTLY": false })
]
};
package.json looks like this
{
"name": "workbench",
"version": "1.0.0",
"description": "My local workbench",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "cd public && serve"
},
"author": "kn#unisport.dk",
"license": "ISC",
"dependencies": {
"babel-core": "^6.7.7",
"babel-preset-stage-1": "^6.5.0",
"babel-preset-stage-2": "^6.5.0",
"fetch": "^1.0.1",
"react": "^0.14.8",
"react-dom": "^0.14.8",
"react-router": "^2.3.0"
},
"devDependencies": {
"babel-core": "^6.5.2",
"babel-loader": "^6.2.4",
"babel-preset-es2015": "^6.5.0",
"babel-preset-react": "^6.5.0",
"bufferutil": "^1.2.1",
"gun": "^0.3.992",
"loader-utils": "^0.2.15",
"url": "^0.11.0",
"utf-8-validate": "^1.2.1",
"webpack": "^2.1.0-beta.5"
}
}
js test code in main.js looks like this
/**
* Main.js
*/
'use strict';
/**
* Setup Gun
* TODO: add peers
*/
var Gun = require('gun');
var gun = Gun();
var React = require('react');
var ReactDom = require('react-dom');
var App = React.createClass({
render() {
return <div>Hello</div>
}
});
var ROOT = document.getElementById('appmount');
ReactDom.render(
<App />,
ROOT
);
but when I load index.html that includes bundle.js I get this error in the console
Uncaught ReferenceError: require is not defined
module.exports = require("url");
/*****************
** WEBPACK FOOTER
** external "url"
** module id = 21
** module chunks = 0
**/
what is it that I'm missing?
Update
Changing node to 'web' as suggested, but this gives me
ERROR in ./~/ws/lib/WebSocketServer.js
Module not found: Error: Can't resolve 'tls' in '/Users/kn/Documents/workbench/node_modules/ws/lib'
# ./~/ws/lib/WebSocketServer.js 15:10-24
ERROR in ./~/diffie-hellman/lib/primes.json
Module parse failed: /Users/kn/Documents/workbench/node_modules/diffie-hellman/lib/primes.json Unexpected token (2:11)
You may need an appropriate loader to handle this file type.
| {
| "modp1": {
| "gen": "02",
| "prime": "ffffffffffffffffc90fdaa22168c234c4c6628b80dc1cd129024e088a67cc74020bbea63b139b22514a08798e3404ddef9519b3cd3a431b302b0a6df25f14374fe1356d6d51c245e485b576625e7ec6f44c42e9a63a3620ffffffffffffffff"
# ./~/diffie-hellman/browser.js 2:13-41
ERROR in ./~/elliptic/package.json
Module parse failed: /Users/kn/Documents/workbench/node_modules/elliptic/package.json Unexpected token (2:9)
You may need an appropriate loader to handle this file type.
| {
| "_args": [
| [
| {
# ./~/elliptic/lib/elliptic.js 5:19-45
ERROR in ./~/parse-asn1/aesid.json
Module parse failed: /Users/kn/Documents/workbench/node_modules/parse-asn1/aesid.json Unexpected token (1:25)
You may need an appropriate loader to handle this file type.
| {"2.16.840.1.101.3.4.1.1": "aes-128-ecb",
| "2.16.840.1.101.3.4.1.2": "aes-128-cbc",
| "2.16.840.1.101.3.4.1.3": "aes-128-ofb",
# ./~/parse-asn1/index.js 2:12-35
Installing tls results in this error
ERROR in ./~/diffie-hellman/lib/primes.json
Module parse failed: /Users/kn/Documents/workbench/node_modules/diffie-hellman/lib/primes.json Unexpected token (2:11)
You may need an appropriate loader to handle this file type.
| {
| "modp1": {
| "gen": "02",
| "prime": "ffffffffffffffffc90fdaa22168c234c4c6628b80dc1cd129024e088a67cc74020bbea63b139b22514a08798e3404ddef9519b3cd3a431b302b0a6df25f14374fe1356d6d51c245e485b576625e7ec6f44c42e9a63a3620ffffffffffffffff"
# ./~/diffie-hellman/browser.js 2:13-41
ERROR in ./~/elliptic/package.json
Module parse failed: /Users/kn/Documents/workbench/node_modules/elliptic/package.json Unexpected token (2:9)
You may need an appropriate loader to handle this file type.
| {
| "_args": [
| [
| {
# ./~/elliptic/lib/elliptic.js 5:19-45
ERROR in ./~/parse-asn1/aesid.json
Module parse failed: /Users/kn/Documents/workbench/node_modules/parse-asn1/aesid.json Unexpected token (1:25)
You may need an appropriate loader to handle this file type.
| {"2.16.840.1.101.3.4.1.1": "aes-128-ecb",
| "2.16.840.1.101.3.4.1.2": "aes-128-cbc",
| "2.16.840.1.101.3.4.1.3": "aes-128-ofb",
# ./~/parse-asn1/index.js 2:12-35
I tried to install primes, but Im getting
ERROR in ./~/diffie-hellman/lib/primes.json
Module parse failed: /Users/kn/Documents/workbench/node_modules/diffie-hellman/lib/primes.json Unexpected token (2:11)
You may need an appropriate loader to handle this file type.
| {
| "modp1": {
| "gen": "02",
| "prime": "ffffffffffffffffc90fdaa22168c234c4c6628b80dc1cd129024e088a67cc74020bbea63b139b22514a08798e3404ddef9519b3cd3a431b302b0a6df25f14374fe1356d6d51c245e485b576625e7ec6f44c42e9a63a3620ffffffffffffffff"
# ./~/diffie-hellman/browser.js 2:13-41
ERROR in ./~/elliptic/package.json
Module parse failed: /Users/kn/Documents/workbench/node_modules/elliptic/package.json Unexpected token (2:9)
You may need an appropriate loader to handle this file type.
| {
| "_args": [
| [
| {
# ./~/elliptic/lib/elliptic.js 5:19-45
ERROR in ./~/parse-asn1/aesid.json
Module parse failed: /Users/kn/Documents/workbench/node_modules/parse-asn1/aesid.json Unexpected token (1:25)
You may need an appropriate loader to handle this file type.
| {"2.16.840.1.101.3.4.1.1": "aes-128-ecb",
| "2.16.840.1.101.3.4.1.2": "aes-128-cbc",
| "2.16.840.1.101.3.4.1.3": "aes-128-ofb",
# ./~/parse-asn1/index.js 2:12-35
Updating once again after changing the code inside main.js
Suggestion from #marknadal did the trick
main.js
/**
* Main.js
*/
'use strict';
/**
* Setup Gun
* TODO: add peers
*/
var Gun = require('gun/gun');
var peers = [
'http://localhost:8080/gun'
];
var gun = Gun(peers);
var React = require('react');
var ReactDom = require('react-dom');
var App = React.createClass({
render() {
return <div>Hello</div>
}
});
var ROOT = document.getElementById('appmount');
ReactDom.render(
<App />,
ROOT
);
And webpack.config
var path = require('path'),
webpack = require('webpack');
module.exports = {
devtool: 'source-map',
target: 'web',
node: {
fs: 'empty'
},
entry: {
workboard: './src/workboard/main.js'
},
output: {
path: __dirname, filename: '/public/[name]/js/bundle.js'
},
module: {
loaders: [
{
test: /.js?$/,
loader: 'babel-loader',
exclude: /node_modules/,
query: {
presets: ['es2015', 'react', 'stage-2', 'stage-1']
}
},
{
test: /\.json$/,
loader: 'json',
include: [
/node_modules/
]
}
],
noParse: [/aws-sdk/]
},
plugins: [
new webpack.DefinePlugin({ "global.GENTLY": false })
]
};
and package.json - it does include a lot more than what's needed for this project, disregard that if you want to attempt to get this running on your own
{
"name": "workbench",
"version": "1.0.0",
"description": "My local workbench",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "cd public && serve"
},
"author": "kn#unisport.dk",
"license": "ISC",
"dependencies": {
"babel-core": "^6.7.7",
"babel-preset-stage-1": "^6.5.0",
"babel-preset-stage-2": "^6.5.0",
"express": "^4.14.0",
"fetch": "^1.0.1",
"react": "^0.14.8",
"react-dom": "^0.14.8",
"react-router": "^2.3.0"
},
"devDependencies": {
"babel-core": "^6.5.2",
"babel-loader": "^6.2.4",
"babel-preset-es2015": "^6.5.0",
"babel-preset-react": "^6.5.0",
"bufferutil": "^1.2.1",
"gun": "^0.3.992",
"json-loader": "^0.5.4",
"loader-utils": "^0.2.15",
"prime": "^0.5.0",
"primes": "0.0.1",
"tls": "0.0.1",
"url": "^0.11.0",
"utf-8-validate": "^1.2.1",
"webpack": "^2.1.0-beta.5"
}
}
Now when I use webpack --watch no warnings or errors are shown. Going to public/workboad and running serve, I see the react application running with no errors
Did #riscarrott 's answer work? I'm the author of gun, and it looks like 1 of the errors is gun related. However I am not a webpack expert so I am unsure what is the problem.
I do know that require('gun') actually loads ./index.js that in turn loads server-side specific logic (which won't work in the browser). If riscarrott 's answer does not work, try replacing require('gun') with require('gun/gun') and see if it works. If this is the case, please file a bug report on https://github.com/amark/gun so we can get this fixed for future people.
If this did not help, several other people on the team and the community use webpack and gun a lot. I'll see if I can get them to reply here.
EDIT: It looks like the de facto way of other projects, like jquery/angular/etc. (https://www.npmjs.com/package/angular) is to have you include them with a < script > tag. Therefore we also recommend you do this as well, as it avoids all these build problems.
<script src="/node_modules/gun/gun.js"></script>
It looks like you're running your code in the browser but you're targeting 'node' so Webpack will leave require statements untouched when referencing builtin modules such as 'url'.
To fix this remove target: 'node'.
My first instinct is maybe you can add a variable that can be detected at build time to maybe overcome the issues...
on https://github.com/petehunt/webpack-howto section 6 (I know there's ways to define like 'ws' to not be pulled, because that will be provided by the browser target; I just don't see it on that page)
On my own project gun was failing to browserify, because of the optional require( 'ws' ) and other things, so I excluded it from packing, and just serve it directly. I also pulled require.js so I could require('gun') at a javascript level, just outside of the package and more in the application of the library-package.
Could also just fall back further to pulling gun using a script tag...

Failed to construct 'WebSocket': The URL '[object Object]' is invalid

I was trying to make a connection to the websocket and get the data in react router. The connection is not happening and getting error Uncaught SyntaxError: Failed to construct 'WebSocket': The URL '[object Object]' is invalid. in the browser console. Using npm start from the terminal to start the application. It is a simple react router application. Below is the react code where I think problem occurred.
import React from 'react'
export default React.createClass({
getInitialState: function() {
return { value : [],
client : '' };
},
componentWillMount: function() {
client = new WebSocket("ws://localhost:8000/","echo-protocol");
client.onerror = function() {
console.log('Connection Error');
};
client.onopen = function() {
function sendNumber() {
if (client.readyState === client.OPEN) {
var number = Math.round(Math.random() * 0xFFFFFF);
client.send(number.toString());
setTimeout(sendNumber, 1000);
}
}
sendNumber();
};
client.onmessage = function(e) {
this.setState({
value: e.data
});
}.bind(this);
},
componentWillUnmount: function(){
client.close();
},
render: function() {
return (React.createElement("div",null,this.state.value))
}
});
The webpack config file is -
module.exports = {
entry: './index.js',
output: {
filename: 'bundle.js',
publicPath: ''
},
module: {
loaders: [
{ test: /\.js$/, exclude: /node_modules/, loader: 'babel-loader?presets[]=es2015&presets[]=react' }
]
}
}
Also, the packge.json file is
{
"name": "react_form",
"version": "1.0.0",
"description": "Sample form that uses React for its rendering",
"main": "index.js",
"scripts": {
"start": "webpack-dev-server --inline --content-base . --history-api-fallback"
},
"author": "J",
"license": "ISC",
"dependencies": {
"react": "^0.14.7",
"react-dom": "^0.14.7",
"react-router": "^2.0.0",
"websocket": "^1.0.23"
},
"devDependencies": {
"babel-core": "^6.5.1",
"babel-loader": "^6.2.2",
"babel-preset-es2015": "^6.5.0",
"babel-preset-react": "^6.5.0",
"http-server": "^0.8.5",
"webpack": "^1.12.13",
"webpack-dev-server": "^1.14.1"
}
}
If any other piece of code is required then please let me know. Also, please find the image of the error coming in the console on selecting the route option. What is the exact issue I am not able to get ?
Try this syntax:
const client = new WebSocket("ws://localhost:8000/");
I was debugging the app in various different ways I could. Then I found that there is no problem with the code. Further changed the name of the file from WebSocket to Socket and route name from webSocket to socket in various files and it worked. The problem was with naming the files and including them into the code accordingly.
On your backend side, if you dont have header with key = "Sec-WebSocket-Protocol" and value of one of your protocols, you will always get this error on chrome.
In my case, it was fine with Firefox

How to use Fine Uploader in React js

Hi i am newbie to Reactjs. i am using fineuploader to upload files to the server.i want to create a FineUploader Component ,so that i can use it where ever i want.
package.json
{
"name": "Sample",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"babel-core": "^6.2.1",
"babel-loader": "^6.2.0",
"babel-preset-es2015": "^6.1.18",
"babel-preset-react": "^6.1.18",
"fine-uploader": "^5.7.1",
"react": "^0.14.6",
"react-dom": "^0.14.6",
"webpack": "^1.12.9",
"webpack-dev-server": "^1.14.0"
}
}
webpack.config.js
var path = require("path");
module.exports = {
entry: [
'./Components/Main.js'
],
output:{
path:__dirname,
filename:'bundle.js'
},
resolve: {
alias: {
'fine-uploader': path.resolve('node_modules/fine-uploader/fine-uploader')
}
},
module:{
loaders: [
{
test: /fine-uploader\.js/,
loader: 'exports?qq'
}
],
loaders:[{
test: /\.jsx?$/,
exclude:/node_modules/,
loader:'babel',
query:{
presets: ['react']
}
}]
}
};
FineUploader.js
import React from 'react';
import qq from 'fine-uploader/fine-uploader';
class FineUploader extends React.Component {
constructor (props) {
super(props)
}
componentDidMount () {
const fu = new qq.FineUploaderBasic({
button: this.refs.fu
})
}
render () {
return <div ref='fu'>Upload!</div>
}
}
export default FineUploader;
Main.js
import React from 'react';
import ReactDOM from 'react-dom';
import FineUploader from './FineUploader.js';
var MainContent = React.createClass({
render:function(){
return (
<div>
<FineUploader />
</div>
);
}
});
ReactDOM.render(<MainContent />,document.getElementById('container'));
When i run the Application i am getting the below error
uncaught TypeError: _fineUploader2.default.FineUploaderBasic is not a constructor
i dint no what i am doing wrong ,Plz somebody guide me
To use Fine Uploader with React you will need to create a new uploader instance within the componentDidMount lifecycle method.
Invoked once, only on the client (not on the server), immediately after the initial rendering occurs. At this point in the lifecycle, you can access any refs to your children (e.g., to access the underlying DOM representation).
Fine Uploader needs an actual rendered DOM element for it to attach event handlers, render other DOM elements as children, etc.
Inside of componentDidMount we not only know our component has been created, but we also have a DOM element and we can reference it as such using a ref.
Here's a tiny example component:
class FineUploader extends React.Component {
componentDidMount () {
const fu = new qq.s3.FineUploaderBasic({
button: this.refs.fu
})
}
render () {
return <div ref='fu'>Upload!</div>
}
}
Even though my answer is not completely relevant to the asked question but I would recommend the react-fine-uploader library to make your life easier.
https://github.com/FineUploader/react-fine-uploader

React-Style, Webpack, React - Uncaught Error: Invariant Violation: The `style` prop

I'm getting the following error in my browser:
Uncaught Error: Invariant Violation: The style prop expects a mapping from style properties to values, not a string. For example, style={{marginRight: spacing + 'em'}} when using JSX.
This is when running webpack-dev-server and going to localhost:8080.
./modules/main.js:
/** #jsx React.DOM */
var React = require('react');
var HoverAction = require('./HoverAction/HoverAction');
var Application = React.createClass({
render: function() {
return (
<div>
<HoverAction title="favorite" />
</div>
);
}
});
if (typeof window !== 'undefined') {
React.render(<Application />, document.getElementById('app'));
}
./modules/HoverAction/HoverActions.js:
/** #jsx React.DOM */
'use strict';
var StyleSheet = require('react-style');
var React = require('react');
var HoverActionStyles = StyleSheet.create({
normal: {
height: '200px',
width: '200px',
border: '1px solid black'
}
});
var HoverActionTitleStyle = StyleSheet.create({
normal: {
textAlign: 'center',
fontSize: '10px'
}
});
var HoverAction = React.createClass({
render: function() {
return (
<div style={HoverActionStyles.normal}>
<div ></div>
<div style={HoverActionTitleStyle.normal} >{this.props.title}</div>
</div>
);
}
});
module.exports = HoverAction;
index.html:
<!doctype html>
<html>
<head>
<link rel="stylesheet" href="bundle.css">
</head>
<body>
<div id="app"></div>
<script src="bundle.js"></script>
</body>
</html>
webpack.config.js:
'use strict';
var ReactStylePlugin = require('react-style-webpack-plugin');
var ExtractTextPlugin = require('extract-text-webpack-plugin');
var webpack = require('webpack');
module.exports = {
devtool: 'sourcemap',
entry: './modules/main.js',
output: {
filename: 'bundle.js',
},
module: {
loaders: [
{
test: /\.js$/,
loaders: [
ReactStylePlugin.loader(),
'jsx-loader?harmony'
]
},
{
test: /\.less$/,
loader: 'style-loader!css-loader!less-loader'
},
{
test: /\.css$/,
loader: ExtractTextPlugin.extract('css-loader')
// loader: 'style-loader!css-loader'
},
{
test: /\.(png|jpg)$/,
loader: 'url-loader?limit=8192'
} // inline base64 URLs for <=8k images, direct URLs for the rest
]
},
plugins: [
new ReactStylePlugin('bundle.css'),
new webpack.DefinePlugin({
'process.env': {
// To enable production mode:
// NODE_ENV: JSON.stringify('production')
}
})
]
};
package.json:
{
"name": "webpack-howto-example",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "webpack-dev-server",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"devDependencies": {
"bundle-loader": "^0.5.4",
"css-loader": "^0.12.0",
"file-loader": "^0.8.1",
"jsx-loader": "^0.13.2",
"less": "^2.5.0",
"less-loader": "^2.2.0",
"react-style": "^0.5.5",
"react-style-webpack-plugin": "0.4.0",
"style-loader": "^0.12.1",
"url-loader": "^0.5.5",
"webpack": "^1.8.10",
"webpack-dev-server": "^1.8.2"
},
"dependencies": {
"react": "^0.13.2",
"react-router": "^0.13.2"
}
}
React Style requires you to use the styles prop instead of style.
var HoverAction = React.createClass({
render: function() {
return (
<div styles={HoverActionStyles.normal}>
<div ></div>
<div styles={HoverActionTitleStyle.normal} >{this.props.title}</div>
</div>
);
}
});
I used the style attribute in JSX file like this
style={{textTransform: 'uppercase'}}
and it worked for me

Resources