How use method isEqual in react - reactjs

I use the method _.isEqual in my function:
const Sidebar = ({ ...props }) => {
function myFunction(codeMenu) {
let menu = null;
const listMenu = props.listMenu;
for(var i = 0; i < listMenu.length; i++){
if(_.isEqual(listMenu[i].code, codeMenu)){
menu = listMenu[i];
}
break;
}
}
}
...
But I have this error:
'_' is not defined no-undef
loadsh is imported in my index.html :
https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js

Instead of using lodash via cdn, use it via npm. Please run following commands:
npm install lodash
and then import it in the file like
import _ from 'lodash';
and then use it. Actually more optimal way to import is:
import isEqual from 'lodash/isEqual';
so that extra lodash package might not be included in the bundle

One reason you may getting that error is that your React bundle is executed before lodash. In that case _ would not have been added to the global scope.
To avoid these kind of issues, I suggest ditching the CDN and instead add lodash as a dependency to your package.json file. You can then cosume lodash as a require or import.
If you import isEqual as import isEqual from 'lodash/isequal' and you're using a bundler such as Webpack, it will not bundle the other lodash functions you're not importing, dramatically reducing the amount of code your browser has to download.

Seems like your _ object is not defined. Did you import lodash like var _ = require('lodash'); ?

You need to import lodash using npm install command
Here's full code how ->
import lodash from lodash
// Load the full build.
var _ = require('lodash');
// Load the core build.
var _ = require('lodash/core');
const Sidebar = ({ ...props }) => {
function myFunction(codeMenu){
let menu = null;
const listMenu = props.listMenu;
for(var i = 0; i < listMenu.length; i++){
if(_.isEqual(listMenu[i].code, codeMenu)){
menu = listMenu[i];
}
break;
}
}

Related

How do I import jStat in a Svelte project

I have a Typescript file in a Svelte project and would like use jStat https://github.com/jstat/jstat like the following:
export namespace Statistics
{
export function cdfNormal (x:number, mean:number = 0, standard_deviation:number = 1)
{
return jStat.normal.cdf(x,mean,standard_deviation);
}
};
I installed it via npm install --save jstat
I tried
import _ from "jstat";
and
var { jStat } = require('jstat')
But both didn't work.
The package says:
Currently jStat is exposed as j$ and jStat inside an object, rather than exported directly. This may confuse some module loaders, however should be easily remedied with the correct configuration.
But just calling jStat doesn't work either. What am I doing wrong?
The package should be imported as:
import jStat from 'jstat';
All the functions (e.g. mean) will be on the this default export object. The import has to be in the file using it.
The package supplies no types and no third party types (#types/jstat) seem to exist. You can type the module manually to any degree you like. E.g. add a file jstat.d.ts like this:
declare module 'jstat' {
const jStat: {
mean: (arr: number[]) => number;
// ...
};
export default jStat;
}
Or just declare it any if you do not mind the lack of type safety.

Error: Zxing functions does not exists. React QR Scanning App

I am trying add a qr code scanning functionality to my react app. I am using #zxing(https://www.npmjs.com/package/#zxing/browser & https://www.npmjs.com/package/#zxing/library) packages.
Following the readme, here's my js code. I have hosted the application on aws so its SSL covered. But I can't seem to figure out the issue. I have read the git repo of both and the functions do exists(https://github.com/zxing-js/browser/tree/master/src/readers)
import React, { useState, useEffect } from "react";
import {
NotFoundException,
ChecksumException,
FormatException
} from "#zxing/library";
import { BrowserQRCodeReader, BrowserCodeReader } from '#zxing/browser';
export default function() {
var qrCodeReader = null;
var codeReader = null;
var sourceSelect = null;
console.log("ZXing code reader initialized");
useEffect(() => {
codeReader = new BrowserCodeReader();
qrCodeReader = new BrowserQRCodeReader();
console.log(codeReader.listVideoInputDevices()); // ISSUE: RETURNS -> listVideoInputDevices() is not a fuction
console.log(qrCodeReader.listVideoInputDevices()); // ISSUE: RETURNS -> listVideoInputDevices() is not a fuction
console.log("Code Reader", codeReader); // ISSUE: SEE IMAGE BELOW
console.log("QR Code Reader", qrCodeReader); // ISSUE: SEE IMAGE BELOW
}, []);
Instead of using the import try using:
`
const zxing = require('zxing-js/library');
`

Importing functions from other files not working (React)

Import:
import { get, set, faviconChange } from '/js/title.js';
title.js
var geta = a => localStorage.getItem(a)
var seta = (a, b) => localStorage.setItem(a, b)
export function get(a) {
localStorage.getItem(a);
}
export function set(a,b) {
localStorage.setItem(a,b);
}
document.title = get('title') || 'Anonymous'
var link = document.createElement('link');
link.rel = 'icon';
document.getElementsByTagName('head')[0].appendChild(link);
export function faviconChange(value) {
set('link', value)
link.href = get('link') || '/favicon.ico';
console.log("working " + link.href)
}
link.href = get('link') || '/favicon.ico';
Error:
./pages/index.js:6:0
Module not found: Can't resolve '/js/title.js'
4 | import Particles from 'react-tsparticles';
5 | import { loadFull } from "tsparticles";
> 6 | import { get, set, faviconChange } from '/js/title.js';
7 |
8 |
9 | export default function Home() {
https://nextjs.org/docs/messages/module-not-found
Whenever I try and import these functions, I get the above error and I've looked it up, and everyone seems to do it this way, yet I get an error. Am I missing something/putting these imports in the wrong file?
Try to import from './js/title.js';
When importing from another file, you must go up in the files hierarchy with a dot './'
3 suggestions are here.
1. adding export in your importing file
export default YourFunctionName;
2. As #SaF mentioned, make sure the path is correct
How?
If you are using linux/unix/mac use the command tree on the parent folder to get the exact path (if windows, you can install tree)
try using ../../../folder_x to go to folder_x which is 3 folders behind and ./ to get current folder
try printing your js file's absolute path using fs module https://www.npmjs.com/package/fs-js (comment the import line and try this)
3. Change jsx file extention to normal js
and add below code in webpack.config.js to resolve jsx as js
module.exports = {
//...
resolve: {
extensions: ['.js', '.jsx']
}
};

Mxgraph Reactjs not import mxgraph library

I try to use mxgraph with react project. But I can not use mxgraph with react.
I install mxgraph:
npm install mxgraph
Then I wrote:
import {
mxGraph,
mxGraphHandler,
mxGraphModel,
...
} from "mxgraph";
InitGraph =()={
let container = document.createElement("div");
this.setContainerStyle();
let model = new mxGraphModel();
let graph = new mxGraph(container,model);
}
I got an error -->
Uncaught TypeError: mxGraphModel is not a constructor.
But when I use to mxgraph-js it is correctly working.
How I use mxgraph ?
I added this piece of code while initializing. This solved the issue
window['mxGraph'] = mxGraph;
window['mxGraphModel'] = mxGraphModel;
const graphEditor = new mxEditor();
window.graphEditor = graphEditor;
graphEditor.createGraph();
let graph = graphEditor.graph
mxgraph to import error to use mxgraph-js ref
https://github.com/cloudfroster/mxgraph-js#readme
install pakage
npm install mxgraph-js --save
import package
import {
mxGraph,
mxGraphHandler,
mxGraphModel,
...
} from "mxgraph-js";
Try this:
npm i --save mxgraph
import MxGraph from "mxgraph/javascript/dist/build.js";
const {
mxGraph,
mxGraphHandler,
mxGraphModel,
...
} = new MxGraph();
Tested this with mxgraph#4.2.2

Sonarqube : Create a custom page using react

I would like to create a custom page using react but I cannot find the documentation to do this. On the Sonarqube documentation, there only the way to create a custom page using javascript only and I don’t understand how the example plugin works with react.
Can you tell me if there is a documentation that I can use.
Short answer: There isn't. There is barely anyone (no one in fact, as far as I've seen) using custom pages currently.
However, it IS possible. You need to create a react project with Webpack (or a similar JS packager).
I also recommend using Create-React-App. This fixes a lot of the setup for you. After that, in your index.js you use the example code from the SonarQube wiki.
Here is an example:
/*
PRODUCTION ENTRYPOINT
*/
import React from 'react';
import ReactDOM from 'react-dom';
import Project from './components/Project';
import './main.css';
window.registerExtension('myplugin/coverage', function (options) {
appendCustomCSS();
let isDisplayed = true;
window.SonarRequest.getJSON('/api/measures/component', {
component: options.component.key,
metricKeys: 'coverage'
}).then(function (response) {
if (isDisplayed) {
let obj = JSON.parse(response.component.measures[0].value);
let div = document.createElement('div');
render(obj, div);
options.el.appendChild(div);
}
});
return function () {
isDisplayed = false;
};
});
function appendCustomCSS() {
let fileref = document.createElement("link");
fileref.setAttribute("rel", "stylesheet");
fileref.setAttribute("type", "text/css");
fileref.setAttribute("href", "/static/myplugin/coverage.css");
document.head.append(fileref);
}
function render(objectArray, container) {
ReactDOM.render(<div className="Coverage"><Project objects={objectArray}/></div>, container);
}

Resources