Electron-React preload - reactjs

Greetings to everyone,
I started working on a React-Electron project and I am stuck on the following error for days,
Error
After I click the button, I want a log, "My custom Notification". Instead, it throws an error. How do I make this work? Maybe I am using it all in a wrong way?
Here is the code that generates that error,
Preload.js
const {ipcRenderer, contextBridge} = require('electron');
contextBridge.exposeInMainWorld('electron', {
notification: (message) => {
console.log(message);
ipcRenderer.send('notify', message);
}
})
main.js
const {BrowserWindow, app} = require('electron');
const path = require('path');
function createWindow() {
const win = new BrowserWindow({
width: 1200,
height: 800,
backgroundColor: 'white',
webPeferences: {
nodeIntegration: false,
worldSafeExecuteJavaScript: true,
contextIsolation: true,
preload: path.join(__dirname, 'preload.js')
}
})
win.loadFile('index.html');
}
app.whenReady().then(createWindow);
require('electron-reload')(__dirname, 'node_modules', '.bin', 'electron');
App.js
import React, {useEffect} from 'react';
import './app.css';
function App () {
return (
<div className='App'>
<h1>Hello React</h1>
<button onClick={() => {
window.electron.notification('My custom Notification');
}}>Click me</button>
</div>
)
}
export default App;
package.json
{
"name": "electron-react-ap",
"version": "1.0.0",
"description": "",
"main": "main.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "electron .",
"watch": "webpack --config webpack.common.js --watch"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"#babel/core": "^7.16.7",
"#babel/preset-env": "^7.16.7",
"#babel/preset-react": "^7.16.7",
"babel-loader": "^8.2.3",
"concurrently": "^7.0.0",
"cross-env": "^7.0.3",
"css-loader": "^6.5.1",
"electron": "^16.0.6",
"electron-reload": "^2.0.0-alpha.1",
"file-loader": "^6.2.0",
"fs": "0.0.1-security",
"nedb": "^1.8.0",
"path": "^0.12.7",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"sass": "^1.47.0",
"sass-loader": "^12.4.0",
"style-loader": "^3.3.1",
"webpack": "^5.65.0",
"webpack-cli": "^4.9.1",
"webpack-dev-server": "^4.7.2"
}
}
Someone help this lost soul

Related

Is there a chrome-dev-tool to diagnose what is being rendered by React.js?

So I just updated from React 16 -> 18.13.0
My code was all working, but after this update, apart from a few "import" errors which I have fixed, now vs code is not posting anything to my browser. The build is compiling with no errors, a new chrome tab is being opened and there are no errors on either console. I'm guessing this may be due to some version incompatibility, but npm doesn't flag anything.
App.js
import React, {Component} from 'react';
import JSONData from './data/data.json';
import './css/style.css';
import General from './components/general/general.js';
import Campaign from "./pages/campaign/campaign.js";
import Services from "./pages/services/services.js";
import Contact from './pages/contact/contact.js';
import {BrowserRouter as Router, Route, Routes} from "react-router-dom";
export class App extends Component {
state = {
landingPageDats: {},
}
getlandingPageData() {
this.setState({landingPageData : JSONData})
}
componentDidMount() {
this.getlandingPageData();
}
render() {
return(
<Router>
<Routes>
<Route path="/" element={<General/>}/>
<Route path="/campaign" element={<Campaign/>}/>
<Route path="/services" element={<Services/>}/>
<Route path="/contacts" element={<Contact/>}/>
</Routes>
</Router>
)
}
}
export default App;
Package.json
{
"name": "trustmotores",
"version": "1.0.0",
"private": true,
"homepage": "https://trustmotores.com",
"proxy": "http://trustmotores.com",
"type": "module",
"jsx": "react-jsx",
"dependencies": {
"#coreui/coreui": "^4.2.2",
"#coreui/react": "^4.4.0",
"#emotion/react": "^11.10.4",
"#emotion/styled": "^11.10.4",
"#material-ui/core": "^4.12.4",
"#material-ui/icons": "^4.11.3",
"#mui/icons-material": "^5.10.9",
"#mui/material": "^5.10.10",
"#testing-library/jest-dom": "^5.11.4",
"#testing-library/user-event": "^12.1.10",
"axios": "^0.21.4",
"babel": "^6.23.0",
"body-parser": "^1.19.0",
"concurrently": "^6.2.1",
"contentful": "^9.2.11",
"cors": "^2.8.5",
"express": "^4.17.1",
"node-pre-gyp": "^0.17.0",
"node-sass": "^8.0.0",
"nodemailer": "^6.6.3",
"pure-react-carousel": "^1.28.1",
"react": "^18.2.0",
"react-awesome-slider": "^4.1.0",
"react-calendar": "^3.9.0",
"react-dom": "^18.2.0",
"react-icons": "^4.2.0",
"react-responsive-carousel": "^3.2.23",
"react-router": "^5.3.1",
"react-router-dom": "^6.3.0",
"react-scripts": "^5.0.1",
"react-select": "^5.6.0",
"react-slick": "^0.28.1",
"react-swipe": "^6.0.4",
"sass": "^1.56.0",
"slick-carousel": "^1.8.1",
"swipe-js-iso": "^2.1.5",
"update": "^0.4.2",
"web-vitals": "^1.0.1",
"yarn": "^1.22.18"
},
"jest": {
"moduleNameMapper": {
"\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/__mocks__/styleMock.js",
"\\.(css|less)$": "<rootDir>/__mocks__/styleMock.js",
"verbose": "true"
}
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "jest",
"eject": "react-scripts eject",
"client": "npm start ",
"server": "nodemon server.js",
"dev": "concurrently \"npm run client\" \"npm run server\" "
},
"eslintConfig": {
"extends": [
"react-app"
]
},
"devDependencies": {
"#babel/core": "^7.15.5",
"#babel/preset-env": "^7.15.6",
"#babel/preset-react": "^7.14.5",
"#react-ui-org/react-ui": "^0.49.0",
"#testing-library/react": "^12.0.0",
"babel-jest": "^27.5.1",
"enzyme": "^3.11.0",
"eslint": "^7.11.0",
"jest": "^27.2.0",
"jest-dom": "^4.0.0",
"jsdom": "20.0.2",
"jsdom-global": "3.0.2",
"nodemon": "^2.0.12",
"react-test-renderer": "^17.0.2",
"swiper": "^8.4.4"
},
"description": "A Trust Motores é uma comercializadora de produtos Auto a nivel nacional.",
"main": "index.js",
"repository": {
"type": "git",
"url": "git+https://github.com/tomascdmota/trustmotores.git"
},
"author": "Tomas Mota",
"license": "MIT",
"bugs": {
"url": "https://github.com/tomascdmota/trustmotores/issues"
},
"transform": {
"\\.js$": "<rootDir>/node_modules/babel-jest"
},
"browserslist": [
">0.2%",
"not dead",
"not ie <= 11",
"not op_mini all"
]
}
Let me know if you need any more info. Thank you

React Navigation Regenerator default async

in the link below have some screen shots of my code (same as the React Navigation official tutorial)
EMU-ERROR
CODE
My json file is:
{
"name": "test",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "node node_modules/react-native/local-cli/cli.js start",
"test": "jest"
},
"dependencies": {
"react": "16.8.3",
"react-native": "0.59.9",
"react-native-gesture-handler": "^1.3.0",
"react-native-vector-icons": "^6.6.0",
"react-navigation": "^3.11.0",
"styled-components": "^4.3.2"
},
"devDependencies": {
"#babel/core": "^7.5.0",
"#babel/runtime": "^7.5.1",
"babel-jest": "^24.8.0",
"jest": "^24.8.0",
"metro-react-native-babel-preset": "^0.55.0",
"react-test-renderer": "16.8.3"
},
"jest": {
"preset": "react-native"
}
}
Any one know how to solve this problem ?
please use this code for navigation
import {
createStackNavigator,
createSwitchNavigator,
createAppContainer,
} from 'react-navigation';
// Auth screens
import Login from './Screen/Login'
import Signup from './Screen/Signup'
// Auth stacks
const AuthStackLogin = createStackNavigator({
Login,
Signup,
},{
headerMode: 'none'
}
);
export default createAppContainer(createSwitchNavigator(
{
AuthStackLogin: {
screen: AuthStackLogin
},
},
{
initialRouteName: 'AuthStackLogin'
}
));

Setup for React styled components

I can't get styled components working; must be something in my setup. Here's the component:
import React from 'react';
import styled from 'styled-components';
const Wrapper = styled.div`
display: flex;
justify-content: center;
margin-top: 100px;
`;
const TestComponent = () => (
<Wrapper>
TEST
</Wrapper>
);
export default TestComponent;
When rendered it's just a <div> with a funky class but no styles.
My package.json:
{
"name": "Lolland",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "webpack-dev-server --open --mode development",
"build": "webpack"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"#babel/runtime": "^7.0.0",
"axios": "^0.18.0",
"babel-plugin-styled-components": "^1.6.0",
"babel-runtime": "^6.26.0",
"history": "^4.7.2",
"mobx": "^5.1.0",
"mobx-react": "^5.2.5",
"mobx-react-router": "^4.0.4",
"mobx-rest": "^2.2.5",
"mobx-rest-axios-adapter": "^2.1.1",
"prop-types": "^15.6.2",
"query-string": "^6.1.0",
"react": "^16.4.2",
"react-dom": "^16.4.2",
"react-router": "^4.3.1",
"react-spinners": "^0.4.5",
"recompose": "^0.30.0",
"styled-components": "^3.4.5"
},
"devDependencies": {
"#babel/core": "^7.0.0",
"#babel/plugin-transform-runtime": "^7.0.0",
"#babel/preset-env": "^7.0.0",
"#babel/preset-react": "^7.0.0",
"babel-loader": "^8.0.0",
"babel-plugin-styled-components": "^1.6.0",
"eslint": "^5.4.0",
"eslint-config-airbnb": "^17.1.0",
"eslint-plugin-import": "^2.14.0",
"eslint-plugin-jsx-a11y": "^6.1.1",
"eslint-plugin-react": "^7.11.1",
"html-loader": "^0.5.5",
"html-webpack-plugin": "^3.2.0",
"webpack": "^4.17.1",
"webpack-cli": "^3.1.0",
"webpack-dev-server": "^3.1.7"
}
}
My .babelrc:
{
"presets": ["#babel/preset-env", "#babel/preset-react"],
"plugins": ["#babel/plugin-transform-runtime", "emotion", "babel-plugin-styled-components"]
}
And my webpack.config.js:
const HtmlWebPackPlugin = require('html-webpack-plugin');
module.exports = {
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
},
},
{
test: /\.html$/,
use: [
{
loader: 'html-loader',
},
],
},
],
},
plugins: [
new HtmlWebPackPlugin({
template: './src/index.html',
filename: './index.html',
}),
],
};
We figured out the answer in the comments together, but for those stumbling over it in the future:
The order of plugins is important. Placing styled-components BEFORE emotion resolves the conflict, since emotion plugin parses the import declarations and does its magic based on that. See the code here. styled-components plugin on the other hand parses the package name, but still uses import declaration, hence the conflict.

React - "Error: Module build failed: SyntaxError: Unexpected token" for a simple function

I am trying out to figure out this error message, here is its occurrence:
import React from 'react';
import ReactDOM from 'react-dom';
import axios from 'axios';
import Moment from 'react-moment';
import LoadModal from './LoadModal';
import Posts from './Posts';
import NewPost from './NewPost';
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
posts: [],
loading: true
};
}
componentDidMount() {
axios.get('/posts')
.then(response => {
this.setState({ posts: response.data, loading: false });
});
}
addPost: function(post) { // <-- HERE's the error
this.setState({ posts : this.state.posts });
}
render() {
return (
<div>
<NewPost addPost={this.addPost}/>
<Posts posts={this.state.posts} loading={this.state.loading} />
</div>
)
}
}
export default App
and the error message is on points to this line:
addPost: function(post) {
I was googling the problem and found out that I might be missing out some parts in the webpack config - this is my package.json:
{
"name": "#rails/webpacker",
"version": "3.2.2",
"description": "Use webpack to manage app-like JavaScript modules in Rails",
"main": "package/index.js",
"files": [
"package",
"lib/install/config/webpacker.yml"
],
"engines": {
"node": ">=6.0.0",
"yarn": ">=0.25.2"
},
"dependencies": {
"babel-core": "^6.26.0",
"babel-loader": "^7.1.2",
"babel-plugin-syntax-dynamic-import": "^6.18.0",
"babel-plugin-transform-class-properties": "^6.24.1",
"babel-plugin-transform-object-rest-spread": "^6.26.0",
"babel-polyfill": "^6.26.0",
"babel-preset-env": "^1.6.1",
"case-sensitive-paths-webpack-plugin": "^2.1.1",
"compression-webpack-plugin": "^1.1.6",
"css-loader": "^0.28.9",
"extract-text-webpack-plugin": "^3.0.2",
"file-loader": "^1.1.6",
"glob": "^7.1.2",
"js-yaml": "^3.10.0",
"node-sass": "^4.7.2",
"path-complete-extname": "^0.1.0",
"postcss-cssnext": "^3.1.0",
"postcss-import": "^11.0.0",
"postcss-loader": "^2.1.0",
"sass-loader": "^6.0.6",
"style-loader": "^0.20.1",
"uglifyjs-webpack-plugin": "^1.1.8",
"webpack": "^3.10.0",
"webpack-manifest-plugin": "^1.3.2"
},
"devDependencies": {
"eslint": "^4.17.0",
"eslint-config-airbnb": "^16.1.0",
"eslint-plugin-import": "^2.8.0",
"eslint-plugin-jsx-a11y": "^6.0.3",
"eslint-plugin-react": "^7.6.1",
"jest": "^22.2.1"
},
"jest": {
"testRegex": "(/__tests__/.*|(\\.|/))\\.jsx?$",
"roots": [
"<rootDir>/package"
]
},
"scripts": {
"test": "jest",
"lint": "eslint {package,lib}/"
},
"repository": {
"type": "git",
"url": "git+https://github.com/rails/webpacker.git"
},
"author": "David Heinemeier Hansson <david#basecamp.com>",
"license": "MIT",
"bugs": {
"url": "https://github.com/rails/webpacker/issues"
},
"homepage": "https://github.com/rails/webpacker"
}
I have added there this block:
"babel": {
"presets": [
"es2015",
"stage-0",
"react"
]
},
but it didn't solve the error, it is still there.
What am I missing yet?
Thank you in advance.
Did you try to write function like this?This will not show any compile time error.
addPost(post) { // <-- HERE's the error
this.setState({ posts : this.state.posts });
}
Try change
addPost: function(post) { // <-- HERE's the error
this.setState({ posts : this.state.posts });
}
to
addPost(post) {
this.setState({ posts : this.state.posts });
}
in ES6 it can be:
addPost = post => {
this.setState({ posts : this.state.posts });
}

wrapper.state() and wrapper.instance().state() are both null react/jest/enzyme

As the subject line says, state is null and I cannot see why.
React 16
Jest
Enzyme
Webpack 4
Babel
(package.json below)
I've hit up pretty much every possible link searchable (although, admittedly I likely just missed 'the one' that tells me what I've done wrong).
If someone responds with a simple 'check here' link and it solves all my problems I'll close this (after I go chew some rocks for a while) and happily move on, but after a few hours of beating myself against the wall I'm close to going back to Jasmine... it at least worked there. :\
Output:
PASS __tests__\Components\ListBox.spec.js
● Console
console.log __tests__\Components\ListBox.spec.js:33
_____ wrapper: null
console.log __tests__\Components\ListBox.spec.js:34
____ instance: null
Test Suites: 1 passed, 1 total
Tests: 2 passed, 2 total
Snapshots: 0 total
Time: 0.985s, estimated 1s
Ran all test suites related to changed files.
Here is my package.json:
{
"name": "module",
"version": "1.0.1",
"author": "thanks all!",
"description": "",
"license": "MIT",
"main": "src/main.jsx",
"jest": {
"automock": true,
"testEnvironment": "jsdom",
"verbose": false,
"setupFiles": [
"raf/polyfill"
],
"moduleNameMapper": {
"\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/__mocks__/fileMock.js",
"\\.(css|less)$": "identity-obj-proxy"
},
"moduleFileExtensions": [
"js",
"jsx",
"es6"
],
"unmockedModulePathPatterns": [
"react",
"react-dom",
"react-addons-test-utils",
"enzyme"
],
"modulePaths": [
"<rootDir>/src",
"<rootDir>/src/app",
"<rootDir>/src/app/Components"
],
"transform": {
"\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga|js|jsx|es6)$": "./node_modules/babel-jest"
}
},
"jestWebpackResolver": {
"webpackConfig": "./webpack.config.js"
},
"scripts": {
"start": "webpack-dev-server --env=dev --colors --hot --mode development -d",
"build": "webpack --env=dev --progress --profile --colors --mode development",
"release": "webpack --env=prod --progress --profile --colors --mode production",
"watch": "webpack --env=dev --profile --colors --watch --watch-poll --mode development",
"lint": "eslint ./src/**.js",
"coverage": "jest --coverage",
"test": "jest",
"test-watch": "jest --coverage --watch --colors",
"testw": "jest --watch --colors"
},
"devDependencies": {
"babel-core": "^6.26.0",
"babel-eslint": "^8.2.2",
"babel-jest": "^22.4.1",
"babel-loader": "^7.1.4",
"babel-preset-env": "^1.6.1",
"babel-preset-stage-3": "^6.24.1",
"core-js": "^2.5.5",
"css-loader": "^0.28.10",
"enzyme": "^3.3.0",
"eslint": "^4.18.2",
"eslint-config-airbnb": "^16.1.0",
"eslint-import-resolver-webpack": "^0.9.0",
"eslint-loader": "^2.0.0",
"eslint-plugin-import": "^2.9.0",
"eslint-plugin-jsx-a11y": "^6.0.3",
"eslint-plugin-react": "^7.7.0",
"identity-obj-proxy": "^3.0.0",
"jest": "^22.4.2",
"jest-enzyme": "^6.0.0",
"jest-webpack-resolver": "^0.3.0",
"node-sass": "^4.5.3",
"object-assign": "^4.1.1",
"raf": "^3.4.0",
"react-hot-loader": "^4.0.0",
"react-test-renderer": "^16.3.2",
"sass-loader": "^6.0.6",
"style-loader": "^0.20.3",
"webpack-dev-server": "^3.1.1",
"webpack-merge": "^4.1.2"
},
"dependencies": {
"babel-preset-react": "^6.24.1",
"clean-webpack-plugin": "^0.1.19",
"enzyme-adapter-react-16": "^1.1.1",
"extract-text-webpack-plugin": "^4.0.0-beta.0",
"flux": "^3.1.3",
"font-awesome": "^4.7.0",
"html-webpack-plugin": "^3.0.6",
"jest-environment-enzyme": "^6.0.0",
"lodash": "^4.17.5",
"moment": "^2.22.0",
"node-rest-client": "^3.1.0",
"npm": "^5.7.1",
"prop-types": "^15.6.1",
"react": "^16.2.0",
"react-dom": "^16.2.0",
"react-router": "^4.2.0",
"react-router-dom": "^4.2.2",
"webpack": "^4.2.0",
"webpack-bundle-analyzer": "^2.11.1",
"webpack-cli": "^2.0.13",
"webpack-dev-middleware": "^3.0.1"
}
}
Here is my spec file:
/*
eslint-disable
react/jsx-filename-extension,
import/first,
no-console
*/
jest.dontMock('enzyme');
jest.dontMock('react');
jest.dontMock('react-dom');
import React from 'react';
import Adapter from 'enzyme-adapter-react-16';
import { configure, shallow, mount } from 'enzyme';
import { ListBox } from 'app/Components/ListBox/ListBox';
configure({ adapter: new Adapter() });
jest.dontMock('app/Components/ListBox/ListBox');
describe('<ListBox {...props} />', () => {
let wrapper;
let list;
const items = [{ text: 'test', key: 1 }];
describe('<ListBox {...props} />', () => {
beforeEach(() => {
wrapper = mount(<ListBox items={items} />);
list = wrapper.instance();
});
it('Can mount without error', () => {
expect(list).toBeInstanceOf(ListBox);
});
it('accepts an array of items to display and stores them in local state.', () => {
console.log(`_____ wrapper: ${wrapper.state()}`);
console.log(`____ instance: ${wrapper.instance().state}`);
});
});
});
Here is the beginning of the component under test:
import React from 'react';
export default class ListBox extends React.Component {
constructor(props) {
super(props);
this.state = {
items: this.props.items
}
}
render() {
return <div>div</div>;
}
}
export { ListBox };
Solved my own problem. Setting automock to false in the package.json and then explicitly mocking/unmocking as needed resolved the issue for me.
Hopefully this helps others in the future.

Resources