map Function Not Found When Reading from JSON File - reactjs

I created a React app with this command: npx create-react-app test-project
I have a small JSON file:
{
"colors": [
{
"id": 0,
"name": "red"
},
{
"id": 1,
"name": "green"
},
{
"id": 2,
"name": "blue"
},
{
"id": 3,
"name": "yellow"
}
]
}
And I am trying to display it in a list in React.
import {colors} from "./colors.json";
function App() {
return (
<div>
<ul>
{colors.map((c, i) =>
<li>{c.name}</li>
)}
</ul>
</div>
);
}
export default App;
When I run it, I get the following error:
Compiled with problems:X
ERROR in ./src/App.js 11:16-26
export 'colors'.'map' (imported as 'colors') was not found in './colors.json' (possible exports: 0, 1, 2, 3)
Because this project is pulled from a larger project, I happen to know what the issue is. In the package.json file, my react-scripts version is 5.0.0. If I downgrade it to 4.0.1, the issue resolves.
However, as someone who is learning React, I would like to understand why this error occurs. I don't understand why it is no longer able to recognize that an array from a JSON file would have a map function defined. I would definitely prefer the behavior found when I set react-scripts to 4.0.1.
Is this a bug or a feature? I have examined the release notes for react-scripts 5.0.0, but don't see anything relevant.

Related

String contains an invalid character when running gatsby develop

I'm building a gatsby web app, I was working with wsl on windows, and it was working well, I even deployed it to a github pages repository, which worked fine, but then I decided I didn't need to use wsl anymore, so I uninstalled it, cloned the git repository and tried to build the app using gatsby develop but in the localhost page it shows this error with no more context:
Unhandled Runtime Error.
One unhandled runtime error found in your files. See the list below to fix it:
Unknown Runtime Error
String contains an invalid character
No codeFrame could be generated
Which isn't particularly useful.
Then, by running the gh-pages command to deploy my page to a github pages repository, I get this error:
ERROR
Page data from page-data.json for the failed page "/": {
"componentChunkName": "component---src-pages-index-js",
"path": "/",
"result": {
"data": {
"allFile": {
"nodes": [
{
"name": "1_corazon-de-maple",
"id": "ac582d47-3543-54cd-83b1-9366ca594fe3",
"childImageSharp": {
"gatsbyImageData": {
"layout": "constrained",
"backgroundColor": "#f8e8d8",
"images": {
"fallback": {
"src": "/static/9acd54f4f5e8efa9b11549c85e00ea4f/f76e2/1_corazon-de-maple.png",
"srcSet": "/static/9acd54f4f5e8efa9b11549c85e00ea4f/2fe1e/1_corazon-de-maple.png 480w,\n/static/9acd54f4f5e8efa9b11549c85e00ea4f/d1fbd/1_corazon-de-maple.png
961w,\n/static/9acd54f4f5e8efa9b11549c85e00ea4f/f76e2/1_corazon-de-maple.png 1921w",
"sizes": "(min-width: 1921px) 1921px, 100vw"
},
"sources": [
{
"srcSet": "/static/9acd54f4f5e8efa9b11549c85e00ea4f/3a3a2/1_corazon-de-maple.webp 480w,\n/static/9acd54f4f5e8efa9b11549c85e00ea4f/e58ca/1_corazon-de-maple.webp
961w,\n/static/9acd54f4f5e8efa9b11549c85e00ea4f/2d899/1_corazon-de-maple.webp 1921w",
"type": "image/webp",
"sizes": "(min-width: 1921px) 1921px, 100vw"
}
]
},
"width": 1921,
"height": 1081
}
}
},
{
"name": "2_appartar",
"id": "12e5156f-1297-5068-a800-2b063db9bf20",
"childImageSharp": {
"gatsbyImageData": {
"layout": "constrained",
"backgroundColor": "#f8f8f8",
"images": {
"fallback": {
"src": "/static/86855c6fbdba7e8237ff47290ac1c15f/eb413/2_appartar.png",
"srcSet": "/static/86855c6fbdba7e8237ff47290ac1c15f/a67ed/2_appartar.png 360w,\n/static/86855c6fbdba7e8237ff47290ac1c15f/379bb/2_appartar.png
720w,\n/static/86855c6fbdba7e8237ff47290ac1c15f/eb413/2_appartar.png 1440w",
"sizes": "(min-width: 1440px) 1440px, 100vw"
},
"sources": [
{
"srcSet": "/static/86855c6fbdba7e8237ff47290ac1c15f/28975/2_appartar.webp 360w,\n/static/86855c6fbdba7e8237ff47290ac1c15f/4b463/2_appartar.webp
720w,\n/static/86855c6fbdba7e8237ff47290ac1c15f/3b606/2_appartar.webp 1440w",
"type": "image/webp",
"sizes": "(min-width: 1440px) 1440px, 100vw"
}
]
},
"width": 1440,
"height": 900
}
}
},
{
"name": "3_sipago",
"id": "65960d9a-741b-5ce9-8519-ddca5800e91d",
"childImageSharp": {
"gatsbyImageData": {
"layout": "constrained",
"backgroundColor": "#f8f8f8",
"images": {
"fallback": {
"src": "/static/3ab66ed0d806a8922cb37b7737185c68/87926/3_sipago.png",
"srcSet": "/static/3ab66ed0d806a8922cb37b7737185c68/a3fa1/3_sipago.png 350w,\n/static/3ab66ed0d806a8922cb37b7737185c68/bc3b9/3_sipago.png
700w,\n/static/3ab66ed0d806a8922cb37b7737185c68/87926/3_sipago.png 1400w",
"sizes": "(min-width: 1400px) 1400px, 100vw"
},
"sources": [
{
"srcSet": "/static/3ab66ed0d806a8922cb37b7737185c68/26a00/3_sipago.webp 350w,\n/static/3ab66ed0d806a8922cb37b7737185c68/f23f0/3_sipago.webp
700w,\n/static/3ab66ed0d806a8922cb37b7737185c68/2c2d0/3_sipago.webp 1400w",
"type": "image/webp",
"sizes": "(min-width: 1400px) 1400px, 100vw"
}
]
},
"width": 1400,
"height": 788
}
}
}
]
},
"site": {
"siteMetadata": {
"title": "Raul Meza Montoya | Portfolio"
}
},
"file": {
"id": "df889c4b-9f80-5d5e-af33-55a721c9d098",
"publicURL": "/static/cd0bd82dbb5f2ff0d8d7ae76b84f470c/cv_raul-meza-montoya.pdf"
}
},
"pageContext": {}
},
"staticQueryHashes": [
"1796249492"
]
}
failed Building static HTML for pages - 1.092s
ERROR #95313
Building static HTML failed for path "/"
See our docs page for more info on this error: https://gatsby.dev/debug-html
15 |
16 | module.exports = _defineProperty;
> 17 | module.exports["default"] = module.exports, module.exports.__esModule = true;
I checked the components in my page and it seems disabling a list of svg components fixed the page, but I can't determine why. the UXUIDesign, FrontEnd, and GraphicDesign are all svg components imported like this:
import FrontEnd from "../images/svgs/front-end_sm.svg";
<ul id="specialty-list" className="list inline-list">
<li>
<UXUIDesign className="svg inline-svg svg-icon" />
<p>Diseño UX/UI</p>
</li>
<li>
<FrontEnd className="svg inline-svg svg-icon" />
<p>Desarrollo Front End</p>
</li>
<li>
<GraphicDesign className="svg inline-svg svg-icon" />
<p>Diseño Gráfico</p>
</li>
</ul>
Update:
Transforming the svg from a regular .svg file to a .js using jsx with transform.tools seems to fix the svgs causing an error, not sure what caused it to fail, and gatsby doesn't specify what went wrong.
I think your issue comes because of the way you are importing the SVG. In Gatsby, this will work:
return <div>
<svg>
{/* some SVG magic code here */}
</svg>
</div>
But this won't:
import SomeSvg from '../svg/path/some-svg.svg'
return <div>
<SomeSvg />
</div>
Because by default, webpack won't interpret SVG files as React components. You can follow this detailed answer: Import SVG as a component in Gatsby but basically, you need to use gatsby-plugin-react-svg. After installing it:
{
resolve: 'gatsby-plugin-react-svg',
options: {
rule: {
include: /svgs/
}
}
}
Note 1: just add the folder that contains the SVG. include is a regular expression (that's why is between slashes, /) so just add the folder name
Note 2: the SVG folder must only contain SVG assets, otherwise the compilation may fail
Then, import it like you were doing:
import FrontEnd from "../images/svgs/front-end_sm.svg";
Move site into allFile query or file query
export const query = graphql`
query {
allFile(
filter: { relativeDirectory: { eq: "thumbnails" } }
sort: { fields: name }
) {
nodes {
name
id
childImageSharp {
gatsbyImageData
}
}
site {
siteMetadata {
title
}
}
}
file(name: {eq: "cv_raul-meza-montoya"}) {
id
publicURL
}
}
`

Firefox add-on host permission error on tab with PDF file

I couldn't find this exact situation elsewhere: I have a custom add-on, which I'm loading temporarily via the developer menu. In the manifest file, I'm asking for a lot of permissions already:
{
"manifest_version": 2,
"name": "...",
"version": "1.0",
"description": "...",
"homepage_url": "...",
"permissions": [
"file:///*",
"*://*/*",
"<all_urls>",
"activeTab",
"bookmarks",
"contextMenus",
"tabs"
],
"background": {
"scripts": ["background.js"]
},
"browser_action": {
"default_icon": {
"19": "icon.svg"
},
"default_title": "..."
}
}
I have a background script only at the moment, however a small content script snippet gets injected:
(function() {
browser.browserAction.onClicked.addListener(async (tab) => {
let executing = browser.tabs.executeScript(tab.id, {
code: "window.getSelection().toString()"
})
...
try {
let selection = await executing
...
} catch(exception) {
console.log(exception)
}
...
})
})()
The purpose is essentially to get the current selection as text (for now) in order to later upload it to a bookmarking service of sorts.
However, when I have a tab open with a PDF file (https://www.example.com/file.pdf or file:///home/foo/file.pdf), the call of the content script to window.getSelection().toString() fails, with the following error:
Error: Missing host permission for the tab
Is this expected and I'm just missing a particular permission (and which one would that be)? Or is it unexpected and possibly worthy of a bug report?
Also, the call works in the developer console, which is why I'm thinking I'm really missing permissions somehow.

Map chart not load in react using fusion chart

I need to integrate map chart using fusion chart library in my react application. after install fusion chart library through yarn configured component including fusion chart library, but when i render the map chart it shows me below error
After having error i just watch the node modules and check the existence of file, but seems like its not there, but my fusion chart installed successfully with no error then how this happened?
Here is my code
import React, { Component } from 'react';
import { Link } from 'react-router-dom';
import FusionCharts from 'fusioncharts';
import Maps from 'fusioncharts/fusioncharts.maps';
import World from 'fusioncharts/maps/fusioncharts.worldwithcountries';
import ReactFC from 'react-fusioncharts';
import FusionTheme from 'fusioncharts/themes/fusioncharts.theme.fusion';
ReactFC.fcRoot(FusionCharts, Maps, World, FusionTheme);
class MapChart extends Component {
state = {
dataSource: {
"chart": {
showCanvasBorder: true,
canvasBorderColor: "#000000",
canvasBorderThickness: 1,
showBorder: true,
borderColor: "#000000",
fillColor: "#f1f1f1",
caption: "* USA traffic not displayed on Heat Map",
includevalueinlabels: "1",
showHoverEffect: false,
showEntityHoverEffect: false,
theme: "fusion",
},
data: [
{
"id": "27",
"value": "8",
"color" : "#f65122"
},
{
"id": "159",
"value": "3",
"color" : "#fcc50b"
},
{
"id": "142",
"value": "3",
"color" : "#f65122"
},
{
"id": "141",
"value": "9",
"color" : "#f3172d"
},
{
"id": "173",
"value": "9",
"color" : "#f3172d"
},
{
"id": "113",
"value": "5",
"color" : "#f65122"
},
{
"id": "193",
"value": "5",
"color" : "#f65122"
},
{
"id": "122",
"value": "9",
"color" : "#f65122"
}
]
}
}
render() {
return (
<React.Fragment>
<div style={{ width: '100%', margin: '20px', textAlign: 'center' }}><button><Link to="/">Back to home</Link></button></div>
<div style={{ textAlign: 'center' }}>
<ReactFC
type="worldwithcountries"
width="80%"
height="500"
dataFormat="JSON"
dataSource={this.state.dataSource} />
</div>
</React.Fragment>);
}
}
export default MapChart;
Any help on this issue would be greatly appreciated. Thanks!
Fusion Maps XT offers interactive maps to plot geographical data like revenue by regions, population by state, survey and election results effectively. You can also drop markers on the map to pinpoint places like office locations and flight routes. It has over 1000 maps including all continents, major countries and all the US states.
Fusion chart library initially not provided any collection of maps. Whenever you install fusion chart library on initial base it has only few maps file in map folder of package. Due to that reason you're file is not available in the whole package. They provided the link on their official page for download all the map related to fusion map chart.
To render these maps, you need to download the map definition files from here and copy-paste the maps folder within your fusion charts directory.
Please follow the steps
open link https://www.fusioncharts.com/download/map-definition-files
click on Download Map Definition Files(https://cdn.fusioncharts.com/downloads/addons/fusionmaps-xt-definition.zip) which contains collection of all the maps available in Fusion Maps XTall the maps available in Fusion Maps XT
after download copy the /maps folder from this download package and paste it in your fusion charts folder.
now run you're existing application the file will be found and your react application will work as expected.
Every information already defined on it official site of fusion chart but not in proper way so with the help of link which earlier defined you'll be directly redirect to that instruction page.
On GitHub i created one repository with map chart using fusionchart library, so you can download that repository and check exact functionality of map chart.
GitHub Repository
Since you are rendering the world with countries map in your app, you need to import fusioncharts.worldwithcounties.js, however, if you install FusionCharts via npm it does not have worldwithcountries file in the map folder, to get the file you need to install fusionmaps, here is the link - https://www.npmjs.com/package/fusionmaps
This will install all the map definition files, then you can use the following command:
import World from 'fusionmaps/maps/fusioncharts.worldwithcountries';

React-Select, how to include the styles css file?

I am trying to use the react-select component (https://github.com/JedWatson/react-select) and confused with this sentence while reading the documentation, see below:
Be sure to include styles at some point, probably during your bootstrapping
import 'react-select/dist/react-select.css';
When I import this line in the React JSX file where I am using the Select component I am getting the following error:
...node_modules/react-select/dist/react-select.css:8\n.Select
{\n^\nParseError: Unexpected token\n>",
"traces": {
"Application Trace": [
{
"id": 39,
"trace": "app/views/layouts/application.html.erb:7:in
`_app_views_layouts_application_html_erb___377503645297909977_70313107001300'"
}
],
"Framework Trace": [
{
"id": 0,
"trace": "browserify-rails (3.3.0) lib/browserify-rails/browserify_processor.rb:220:in `run_browserify'"
},
{
"id": 1,
"trace": "browserify-rails (3.3.0) lib/browserify-rails/browserify_processor.rb:51:in `call'"
},
{
....
}
Can someone provide some hints on this issue?
It looks like I found an answer towards this question.
Basically what I did is I manually copy the css file from node_modules/react-select/dist/react-select.css into /assets/javascript/stylesheets folder in my rails project.
For more infomation please look into: https://stackoverflow.com/a/10291177/5761338

install from github using composer and no composer.json in github

I try to install the plugin CakePHP-CSV using Composer. I forked it from ProLoser/CakePHP-CSV.
I don't want to install it in Vendor, but in Plugin folder and with the name of Csv and not the default one that is the plugin name. I tried to use the extra in composer.json but what ever I tried it installs it in Vendor folder of the project.
"repositories": [
{
"type": "package",
"package": {
"name": "CakePHP-CSV/Csv",
"version": "dev-master",
"source": {
"url": "https://github.com/sela/CakePHP-CSV",
"type": "git",
"reference": "origin/master"
}
}
}
],
"require": {
"CakePHP-CSV/Csv": "dev-master",
},
"config": {
"vendor-dir": "Vendor"
},
"extra": {
"installer-paths": {
"Plugin/Cvs": ["Plugin/cvs"]
}
}
You need to make your package depend on composer/installers.
From Composer's documentation:
If you are a package author and want your package installed to a custom directory, simply require composer/installers and set the appropriate type. This is common if your package is intended for a specific framework such as CakePHP, Drupal or WordPress. Here is an example composer.json file for a WordPress theme:
{
"name": "you/themename",
"type": "wordpress-theme",
"require": {
"composer/installers": "~1.0"
}
}
And later (I've added italics to the important part):
As a package consumer you can set or override the install path for a package that requires composer/installers by configuring the installer-paths extra. A useful example would be for a Drupal multisite setup where the package should be installed into your sites subdirectory.
Note that extra is simply a place to put arbitrary data:
Arbitrary extra data for consumption by scripts.
This can be virtually anything. To access it from within a script event handler, you can do:
$extra = $event->getComposer()->getPackage()->getExtra();
The second part is very important; something has to retrieve the extra data and do something with it. In this instance that something is composer/installers.
I needed more space for the code so I answer myself. The following code worked and I didn't need to fork the code.
"repositories": [
{
"type": "package",
"package": {
"name": "ProLoser/CakePHP-CSV",
"version": "1.0",
"type": "cakephp-plugin",
"dist": {
"url": "https://github.com/ProLoser/CakePHP-CSV/archive/master.zip",
"type": "zip"
}
}
}
],
"require": {
"composer/installers": "~1.0.0",
"ProLoser/CakePHP-CSV": "1.*"
},
"config": {
"vendor-dir": "Vendor"
}

Resources