How to put FontAwesome icons in react? - reactjs

I created one React project and I am trying to put FontAwesome icons in react project, but it is showing some syntax errors so help me to resolve this issue.
This is package.json file
{
"name": "icons",
"version": "0.1.0",
"private": true,
"dependencies": {
"#fortawesome/fontawesome-svg-core": "^1.2.25",
"#fortawesome/free-brands-svg-icons": "^5.11.2",
"#fortawesome/free-regular-svg-icons": "^5.11.2",
"#fortawesome/free-solid-svg-icons": "^5.11.2",
"#fortawesome/react-fontawesome": "^0.1.7",
"react": "^16.11.0",
"react-dom": "^16.11.0",
"react-scripts": "3.2.0"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
This is index.js file
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import * as serviceWorker from './serviceWorker';
import { FontAwesomeIcon } from '#fortawesome/react-fontawesome'
import { faCoffee } from '#fortawesome/free-solid-svg-icons'
const element = <FontAwesomeIcon icon={faCoffee} />
ReactDOM.render(<App />, element, document.getElementById('root'));
serviceWorker.unregister();
I am getting error like this Error: Target container is not a DOM element.

You got a little confused with rendering your React code into the DOM.
React.render() takes 2 parameters, the first is your React element, and the second is the target in your index.html file, whereas you have added 3 parameters, hence the error.
You would need to create a React stateless functional component in this scenario, called App:
function App() {
return (
<div>
<FontAwesomeIcon icon={faCoffee} />
</div>
)
}
And now the jsx component is now available as per your existing code, so you can use:
ReactDOM.render(<App />, document.getElementById('root'));
Sources: https://reactjs.org/docs/components-and-props.html

If you're not rendering the icon in App, you can render it by itself directly:
const element = <FontAwesomeIcon icon={faCoffee} />
ReactDOM.render(<div>{element}</div>, document.getElementById('root'));
But as Kraylog pointed out you are not using the ReactDOM.render method correctly. You can read about it here.

As to your "I am getting error like this Error: Target container is not a DOM element." Error, you need to make sure that in your index.html file there is a div with an id of root like such <div id="root"></div>. ReactDOM (which is imported from import ReactDOM from "react-dom";) has a render method on it that converts your React code to plain JavaScript and then inserts it into the real DOM. The render method ONLY NEEDS TWO arguments to work, however a third argument can be used as a callback function like such ReactDOM.render(element, container, () => console.log("Application Mounted in DOM")), but I've never seen anyone use the callback function. You can read more about it here on the official document website ReactDOM.render, or you can watch a pretty decent video on it on YouTube here
Here are some further tips on using the icons in a reusable manner.
Follow these steps taken from the #fortawsome npmjs registry
Install the necessary packages for the cause
$ npm i --save #fortawesome/fontawesome-svg-core
$ npm i --save #fortawesome/free-solid-svg-icons
$ npm i --save #fortawesome/react-fontawesome
$ yarn add #fortawesome/fontawesome-svg-core
$ yarn add #fortawesome/free-solid-svg-icons
$ yarn add #fortawesome/react-fontawesome
Install some MORE of the free stuff in that they were nice enough to provide :)
$ npm i --save #fortawesome/pro-solid-svg-icons
$ npm i --save #fortawesome/pro-regular-svg-icons
$ npm i --save #fortawesome/pro-light-svg-icons
$ npm i --save #fortawesome/pro-duotone-svg-icons
Install even more if you are a paid pro member
$ npm i --save #fortawesome/pro-solid-svg-icons
$ npm i --save #fortawesome/pro-regular-svg-icons
$ npm i --save #fortawesome/pro-light-svg-icons
$ npm i --save #fortawesome/pro-duotone-svg-icons
Usage
Now, finally to the good part of actually using it in an application.
I'm going to create a wrapper component around it, so that it's easy to reuse the icon/component later on, and in order to do this start by creating a file called CoffeeIcon.js, and in that file put in something along the lines of the following:
import React from "react";
import { FontAwesomeIcon } from "#fortawesome/react-fontawesome";
import { faCoffee } from "#fortawesome/free-solid-svg-icons";
import PropTypes from "prop-types";
const CoffeeIcon = props => {
return (
<React.Fragment>
{props.labelText ? <label>{props.labelText}</label> : null}
<FontAwesomeIcon
icon={faCoffee}
style={props.styles}
id={props.id}
className={props.className}
/>
</React.Fragment>
);
};
CoffeeIcon.propTypes = {
labelText: PropTypes.string,
id: PropTypes.string,
className: PropTypes.string,
styles: PropTypes.object
};
export default CoffeeIcon;
If you plan on using PropTypes for type checking and validation then add it to your application like such:
yarn add prop-types or npm i prop-types
And Finally, back in your component where you desire to use it, just do the following:
import it and place it just below your other import statements:
import CoffeeIcon from "../Components/CoffeeIcon";
Drop in the component where you desire:
<CoffeeIcon
styles={{ color: "maroon" }} // optional
id={"coffee-wrapper-id" || null} // optional
className={"coffee-wrapper-class" || null} // optional
labelText={"Coffee Icon"} // optional
/>
You're done and can now EASILY reuse it!

Related

Spread operator causing "Attempted import error" in `npm run build`, but development mode compiles fine

I'm receiving the following error after running npm build run which goes away after removing the line with the spread operator:
> react-scripts build
Creating an optimized production build...
Failed to compile.
./src/Footer.js
Attempted import error: 'getText' is not exported from './Getters'.
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! test-redux-app#0.1.0 build: `react-scripts build`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the test-redux-app#0.1.0 build script.
npm ERR! This is probably not a problem with npm. There is likely additi
onal logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! /Users/myName/.npm/_logs/2020-08-24T22_14_07_926Z-deb
ug.log
App.js (main app cleaned up from npx create-react-app my-app --template redux)
Footer.js (Component)
Getters.js (selector file)
App.js
import React from 'react';
import Footer from './Footer';
import { getText } from './Getters';
function App() {
return <Footer/>
}
export default App;
Footer.js
import React from 'react';
import { useSelector } from 'react-redux';
import { getText } from './Getters';
function Footer(){
let text = useSelector( state =>
getText( state, 'menuName' )
)
return <h1>{text}</h1>;
}
export default Footer;
Getters.js
let Getters = {}
Getters.thisFunctionIsCausingTrouble = function( state, tableName ){
let toRet = { ...state };
return toRet;
}
Getters.getText = function( state ){
return 'Text';
}
module.exports = Getters;
Everything works perfectly fine on localhost, but when I try to build it just screws up.
Again, when I remove the spread operator of the other function, it builds fine.
Another strange result is that if I remove the import Footer and Footer call from the app then I get a successful build. (The getText import remains in App.js).
Why the build fail?
package.json
{
"name": "test-redux-app",
"version": "0.1.0",
"private": true,
"dependencies": {
"#reduxjs/toolkit": "^1.1.0",
"#testing-library/jest-dom": "^4.2.4",
"#testing-library/react": "^9.3.2",
"#testing-library/user-event": "^7.1.2",
"react": "^16.13.1",
"react-dom": "^16.13.1",
"react-redux": "^7.1.3",
"react-scripts": "3.4.3"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
Convert the export from es6 to .jsx. module.exports is a es6 version of exporting. I had similar problem. Fixed it by adding export in front of the function name. ex: export const getheader = () => {...}
then You should be able to import it the way you currently are.

React Native - Get an error when using a custom component that I made (Error: undefined Unable to resolve module <MyModule>)

I made a custom component called "react-native-weekly-calendar",
and I'm trying to publish it to open source community,
but I want to test it first. When I try to test it by npm install --save ../<component_name>, it throws an error.
My directory structure:
- react-native-weekly-calendar (folder)
- index.js
- package.json
- src (folder)
- Locale.js
- Style.js
index.js:
import React, { useState, useEffect, useRef } from 'react';
import { Text, View, ScrollView, TouchableOpacity, TouchableWithoutFeedback, Modal, Platform, ActivityIndicator } from 'react-native';
import PropTypes from 'prop-types';
import moment from 'moment/min/moment-with-locales';
import DateTimePicker from '#react-native-community/datetimepicker';
import { FontAwesome } from 'react-native-vector-icons';
import { applyLocale, displayTitleByLocale } from './src/Locale';
import styles from './src/Style';
const WeeklyCalendar = props => {
...
}
export default WeeklyCalendar;
package.json:
{
"name": "react-native-weekly-calendar",
"version": "0.1.0",
"description": "Weekly Calendar component for React Native",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/codeinjuice/react-native-weekly-calendar.git"
},
"keywords": [
"react",
"reactnative",
"react-native",
"react-native-component",
"calendar",
"weeklycalendar",
"weekly-calendar",
"scheduler",
"datepicker",
"date-picker"
],
"author": "codeinjuice",
"license": "MIT",
"bugs": {
"url": "https://github.com/codeinjuice/react-native-weekly-calendar/issues"
},
"homepage": "https://github.com/codeinjuice/react-native-weekly-calendar#readme",
"dependencies": {
"moment": "^2.24.0",
"prop-types": "^15.5.7",
"#react-native-community/datetimepicker": "~2.1.0",
"react-native-vector-icons": "~6.6.0"
},
"devDependencies": {
"#babel/core": "^7.0.0",
"metro-react-native-babel-preset": "^0.58.0",
"react": "~16.9.0",
"react-native": "0.61.4"
}
}
Here is how I tested with the files above:
$ npx react-native init sample
$ cd sample
$ npm install --save ../react-native-weekly-calendar
$ vim App.js
Then I added import WeeklyCalendar from 'react-native-weekly-calendar'; in App.js.
Finally I ran
$ npx react-native run-ios
In the simulator, I get this error:
Unable to resolve module 'react-native-weekly-calendar' from 'App.js':react-native-weekly-calendar could not be found within the project.
Did I set my dependency settings wrong in package.json???
I don't understand why it keeps saying the component is not found when it's clearly inside of node_modules folder.
Any suggestions are welcomed!
Edit: I upgraded react-native from 0.60.* to 0.61.4 and it worked with npm as well.
Original answer
I used $ yarn add ../react-native-weekly-calendar
instead of $ npm install --save ../react-native-weekly-calendar and it worked.

Module not found: Can't resolve 'bootstrap/dist/css/bootstrap.css' in 'C:\Users\test\counter-app\src'

I'm new to react js. I'm trying to create a component. For that I've installed bootstrap (version 4.1.1) which is successfully installed.
Then I imported the same in index.js and while reloading the page (localhost:3000) I got the error msg:
Module not found:
Can't resolve 'bootstrap/dist/css/bootstrap.css' in 'C:\Users\test\counter-app\src'
My index.js:
import React from "react";
import ReactDOM from "react-dom";
import "./index.css";
import App from "./App";
import * as serviceWorker from "./serviceWorker";
import "bootstrap/dist/css/bootstrap.css";
ReactDOM.render(<App />, document.getElementById("root"));
serviceWorker.unregister();
My package.json :
{
"name": "counter-app",
"version": "0.1.0",
"private": true,
"dependencies": {
"react": "^16.8.4",
"react-dom": "^16.8.4",
"react-scripts": "2.1.8"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": [
">0.2%",
"not dead",
"not ie <= 11",
"not op_mini all"
]
}
Please help me to fix this.
Change import path to bootstrap.min.css like this:
import 'bootstrap/dist/css/bootstrap.min.css';
This occurs either because the installation of a library wasn't successful, i.e. bootstrap had an issue installing within your node_modules directory, or your reference to the library within your code is incorrect. Can you verify that your bootstrap installation was correct by looking within your node_modules for a bootstrap folder?
This would be at <base_path>/node_modules/bootstrap where <base_path> is your project's root directory location. For you I believe the absolute path is the following:
C:\Users\test\counter-app\src\node_modules\bootstrap
If you don't see the bootstrap directory please do the following:
npm i -S bootstrap
as this will save bootstrap as dependency for your project. Your reference to bootstrap within index.js looks fine: import 'bootstrap/dist/css/boostrap.css'; With that being said, a lot of bootstrap's features depend on the following libraries: popper.js, and jquery. So once you get your installation issue resolved you have to install these modules as well and reference them appropriately within your index.js. Something along the lines of:
Install modules:
npm i -S popper.js
npm i -S jquery
index.js
import 'jquery/dist/jquery.js';
import 'popper.js/dist/umd/popper.js';
import 'boostrap/dist/js/bootstrap.js';
Hopefully that helps!
Using bootstrap like that is not really recommended. You should look into react-bootstrap. You would need to run:
npm install react-bootstrap bootstrap
and then:
import Button from 'react-bootstrap/Button';
// or less ideally
import { Button } from 'react-bootstrap';
Check out their documentation for more.
I had the same problem but managed to solve it using these steps:
Remove the node_modules folder
Install bootstrap by running npm install bootstrap
Install other packages by running npm install.
Beware of confusing 'boostrap' with 'bootsrap'
Theese, install different packages:
$ npm install boostrap
$ npm install bootstrap
I was requiring the second. Because of that, I've got question's error.

"babel-plugin-react-intl" is not working as expected

I have created my react app using create-react-app command. Now I would like to available my website in both hindi and english language.
So for that I am using yahoo/react-intl. I have also installed the babel-plugin-react-intl plugin. Also added the .babelrc file.
My .babelrc file look like:
{
"presets": ["es2015", "react"],
"plugins": [
[
"react-intl",
{
"messagesDir": "./build/messages/"
}
]
]
}
But I am not getting the extracted messages from my js file to ./build/messages/.
I dont know where I made a mistake.
My index.js is:
import React from "react";
import ReactDOM from "react-dom";
import { IntlProvider, addLocaleData } from "react-intl";
import hi from "react-intl/locale-data/hi";
import en from "react-intl/locale-data/en";
import registerServiceWorker from "./registerServiceWorker";
import Hello from "./components/Hello";
addLocaleData([...en, ...hi]);
ReactDOM.render(
<IntlProvider locale="hi" messages={/*comming soon*/}>
<Hello />
</IntlProvider>,
document.getElementById("root")
);
registerServiceWorker();
And all my js-files are inside the components folder.
Foe example my Hello.js component looks like:
import React, { Component } from "react";
import { FormattedMessage } from "react-intl";
class Hello extends Component {
render() {
return (
<div>
<h1>
<FormattedMessage id="Hello.heading" defaultMessage="Hello world" />
</h1>
<p>
<FormattedMessage
id="Hello.paragraph"
defaultMessage="This is my world"
/>
</p>
</div>
);
}
}
export default Hello;
My package.json is:
{
"name": "i18n-myexamplae",
"version": "0.1.0",
"private": true,
"dependencies": {
"react": "^16.4.2",
"react-dom": "^16.4.2",
"react-intl": "^2.4.0",
"react-scripts": "1.1.4"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
},
"devDependencies": {
"babel-plugin-react-intl": "^2.4.0"
}
}
This is because react-scripts don't use your .babelrc. You either need to eject because then you're allowed to edit .babelrc config or you need to run babel manually.
Run ./node_modules/.bin/babel src --out-dir lib and ignore/delete everything in --out-dir. You're doing this only to extract messages. Also, to make build consistent, replace your es2018 and react presets with react-app preset, which is the one used by create-react-app.
NOTE: when using react-app preset you actually need to run NODE_ENV=development ./node_modules/.bin/babel src --out-dir lib, because react-scripts require NODE_ENV to be configured. You can add this to you package.json to make things easier.
"scripts": {
"extract": "NODE_ENV=development babel src --out-dir lib",
...
},
I faced a similar issue in i18n library I'm writing, lingui. lingui extract command is checking if a project uses create-react-app and if so, it uses react-app preset automatically for extracting messages.

NPM + React + TypeScript with AMD modules

I'm trying to setup dev environment for app written in typescript using react.
I alreally have existing typescript code that compiles to ES5 and AMD modules.
I would like to avoid any js transpilation in the browser. Please notice, that I don't need to use babel to transpile jsx, since typescript compiles tags into React.createElement in tsx files.
I've done the usual:
npm install react -save
npm install react-dom -save
npm install typescript -save-dev
npm install typings -save-dev
typings install react --ambient --save
typings install react-dom --ambient --save
now, I have two questions:
how to correctly import/require the react and react-dom in my tsx files which compiles to ES5/AMD?
currently I'm just doing this:
/// <reference path="../../typings/browser/ambient/react-dom/index.d.ts" />
/// <reference path="../../typings/browser/ambient/react/index.d.ts" />
export class MyComponent extends __React.Component<any, {}> {
public render() {
return (
<h1>MyCompoentnt</h1>
);
}
}
however the compilations fails with error TS2304: Cannot find name 'React'.
how to include react.js and react-dom.js in my app? I quess some transpilation or browserification will be needed. Or should I just and them to index.html?
<script src="scripts/react.js"></script>
<script src="scripts/react-dom.js"></script>
<script src="scripts/require.min.js" data-main="scripts/app"></script>
here is my package.json:
{
"name": "myhtmlapp",
"version": "1.0.0",
"description": "react test app",
"main": "index.js",
"dependencies": {
"jquery": "^2.2.3",
"react": "^15.0.0",
"react-dom": "^15.0.0"
},
"devDependencies": {
"browser-sync": "^2.11.2",
"typescript": "^1.8.9",
"typings": "^0.7.12"
},
"scripts": {
"watch:typescript": "tsc --p ./appscripts -w",
"watch:css": "browser-sync start --server --files .wwwroot/css/*.css",
"compile": "tsc --p ./appscripts",
"test": "echo \"Error: no test specified\" "
},
"keywords": [],
"author": "",
"license": "ISC"
}
here is entire app: https://onedrive.live.com/redir?resid=51A46BBA4E9EF07E!263323&authkey=!AKQ6FqjE2W2YVBo&ithint=file%2czip
I am pretty sure there a several ways to do this. Below I am posting my way of getting things you have mentioned play together:
To import react I use standard import, like this:
import * as React from 'react';
import * as ReactDOM from 'react-dom';
export class MyComponent extends React.Component<any, any>
{
//...
}
To 'inculde' react into the application I use systemjs as module loader (due to some reasons that goes beyong the question I cant use browserify, but pretty sure it can be used to do the same). You will need to install systemjs via npm. Then instruct it to know where to look for react. To do this put the following script in your index.html:
<script src="systemjs/dist/system.src.js"></script>
<script>
System.config({
baseURL: './lib',
paths: {
"react*": 'react/dist/react-with-addons'
}
});
System.defaultJSExtensions = true;
</script>
Where react/dist/react-with-addons is a path (without '.js' extension) to the dist of react-with-addons.js. And systemjs/dist/system.src.js - path to systemjs dist.
Hope this helps.

Resources