get local image path from json in react - reactjs

How to pick the local path of image
Json
"avatarUrl": "avt1.jpg"
All the images are under src>img folder.
I am looking for absolute path + image name from json.
How I can achieve this
reactJs
<img src={require('./img/avt1.jpg')} width="60" />
Package.js
{
"name": "lummdb",
"version": "0.1.0",
"private": true,
"dependencies": {
"moment-timezone": "^0.5.14",
"react": "^16.0.0",
"react-dom": "^16.0.0",
"react-fontawesome": "^1.6.1",
"react-scripts": "1.0.16",
"axios": "^0.17.0"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
},
}

Seems you are using create-react-app , try to add NODE_PATH=src in your environment variable , or append it to script alias
"start": "NODE_PATH=src react-scripts start",
If you are done, you can do the following :
<img src={require('img/avt1.jpg')} width="60" />
Then, if you are getting the name of image from json dynamically :
<img src={require(`img/${avatarUrl}`)} width="60" />
If does not work, consider to export NODE_PATH before :
export NODE_PATH=src;
npm start;

Move all of your images to the public/images.
JSON file:
{
"title": "something",
"image_link": "../images/yourfilename1.jpg"
},
{
"title": "something2",
"image_link": "../images/yourfilename2.jpg"
}
CardComponent:
const InfoCard = ({image_link, title}) => {
return (
<div>
<h3>{title}</h3>
<img src={image_link} alt={title} />
</div>
)
}
export default InfoCard;

Related

Algolia's searchBox is stuck and is not accepting new values

I'm trying to type new values ​​after instantiating Algolia's SearchBox, but it doesn't change the input value at all. I've recreated the NextJs boilerplate and implemented it 5 times, but the value never changes. What could it be?
In short: when I type something in the input, the value just doesn't update
Github:
https://github.com/Arthur-Maskalenkas/bug-report
sandbox:
https://codesandbox.io/p/github/Arthur-Maskalenkas/bug-report/csb-zf1foq/draft/happy-smoke?file=%2FREADME.md
my currently index.tsx is
import type { NextPage } from 'next'
import algoliasearch from 'algoliasearch/lite';
import {
InstantSearch,
SearchBox,
Pagination,
Configure,
} from 'react-instantsearch-dom';
const Home: NextPage = () => {
const searchClient = algoliasearch(
'B1G2GM9NG0',
'aadef574be1f9252bb48d4ea09b5cfe5'
);
return (
<div className="ais-InstantSearch">
<h1>React InstantSearch e-commerce demos</h1>
<InstantSearch searchClient={searchClient} indexName="demo_ecommerce">
<Configure hitsPerPage={8}/>
<div className="search-panel">
<div className="search-panel__filters">
</div>
<div className="search-panel__results">
<SearchBox
className="searchbox"
translations={{
placeholder: ''
}}
/>
<div className="pagination">
<Pagination/>
</div>
</div>
</div>
</InstantSearch>
</div>
)
}
export default Home
my package
{
"name": "teste2",
"version": "0.1.0",
"private": true,
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start",
"lint": "next lint"
},
"dependencies": {
"algoliasearch": "^4.14.2",
"next": "12.3.1",
"react": "18.2.0",
"react-dom": "18.2.0",
"react-instantsearch-dom": "^6.33.0"
},
"devDependencies": {
"#types/node": "18.7.20",
"#types/react": "18.0.21",
"#types/react-dom": "18.0.6",
"#types/react-instantsearch-dom": "^6.12.3",
"eslint": "8.24.0",
"eslint-config-next": "12.3.1",
"typescript": "4.8.3"
}
}
There is a known issue with React 18.x strict mode and older InstantSearch libraries. The resolution is to set reactStrictMode: false in next.config.js (not recommended), downgrade to React 17, or move to the newer react-instantsearch-hooks library as documented here: https://github.com/algolia/react-instantsearch/issues/3634

Issue with .env being undefinde when using create react app --template typescript?

I am having an issue defining env file in react app using the npx create-react-app my-app --template typescript, below are all the needed info on how the structure is shouldn't the env file work right out of the box ? or is this some new issue ? , basically I didn't touch any think after creating the app just put the console log in the app js so I could see the behavour.
import React from 'react';
import logo from './logo.svg';
import './App.css';
function App() {
console.log(process.env);
return (
<div className='App'>
<header className='App-header'>
<img src={logo} className='App-logo' alt='logo' />
<p>
Edit <code>src/App.tsx</code> and save to reload.
</p>
<a className='App-link' href='https://reactjs.org' target='_blank' rel='noopener noreferrer'>
Learn React
</a>
</header>
</div>
);
}
export default App;
// .env file
REACT_APP_PAYPAL_CLIENT_ID : AWLe2ytIee-lQTYPM_1v1fSnXxiJj-xDgr0xngrbX2vIDNA0zPw_028LsGlLBStEeHhTFzsnLjvqIPij
// Package
{
"name": "my-app",
"version": "0.1.0",
"private": true,
"dependencies": {
"#testing-library/jest-dom": "^5.16.1",
"#testing-library/react": "^12.1.2",
"#testing-library/user-event": "^13.5.0",
"#types/jest": "^27.4.0",
"#types/node": "^16.11.21",
"#types/react": "^17.0.38",
"#types/react-dom": "^17.0.11",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-scripts": "5.0.0",
"typescript": "^4.5.5",
"web-vitals": "^2.1.3"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
From the Adding Development Environment Variables In .env,
The format should be key=value, NOT key: value. Replace : to =
.env:
REACT_APP_PAYPAL_CLIENT_ID=AWLe2ytIee-lQTYPM_1v1fSnXxiJj-xDgr0xngrbX2vIDNA0zPw_028LsGlLBStEeHhTFzsnLjvqIPij

React bulma components import is not working

I have created the react app using CRA (CREATE-REACT-APP). I have installed node-sass and renamed App.css to App.scss and updated its import in App.js. Now in my App.js, I am just trying to print some text inside a section.
This is the link the repo, I am talking about. For your reference
node -v v12.11.1 and npm -v 6.11.3
App.js
import React from 'react';
import './App.scss';
import Section from 'react-bulma-components/lib/components/section';
function App() {
return (
<Section>
<div class="container">
<h1 class="title">
Hello World
</h1>
<p class="subtitle">
My first website with <strong>Bulma</strong>!
</p>
</div>
</Section>
);
}
export default App;
App.scss
#import 'react-bulma-components/src/index.sass';
package.json
{
"name": "just-trying",
"version": "0.1.0",
"private": true,
"dependencies": {
"node-sass": "^4.13.0",
"react": "^16.11.0",
"react-bulma-components": "^3.1.3",
"react-dom": "^16.11.0",
"react-router-dom": "^5.1.2",
"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"
]
},
"devDependencies": {
"sass-loader": "^8.0.0"
}
}
Instead of downgrading the version of node, I have uninstalled react-bulma-components and instead just installed bulma via npa like npm i bulma.. and that works fine.

ReactJS: Can't resolve '#babel/runtime/helpers/builtin/interopRequireDefault'

I followed the tutorial in online video but I got an error code like this:
Failed to compile
./node_modules/#material-ui/icons/Menu.js Module not found: Can't resolve '#babel/runtime/helpers/builtin/interopRequireDefault' inb'C:\LocalServer\static-page\node_modules\#material-ui\icons'
This error occurred during the build time and cannot be dismissed.
This is the code I write:
import React, {Component} from 'react';
import AppBar from '#material-ui/core/AppBar';
import ToolBar from '#material-ui/core/Toolbar';
import MenuIcon from '#material-ui/icons/Menu';
import IconButton from '#material-ui/core/IconButton';
class Header extends Component{
render(){
return(
<div>
<AppBar position="fixed" style={{backgroundColor:'#2f2f2f',boxShadow:'none',padding:'10px 0px'}}>
<ToolBar>
<div className="header_logo">
<div className="font_righteous header_logo_venue">The Venue</div>
<div className="header_logo_title">MUSICAL EVENTS</div>
</div>
<IconButton aria-label="Menu" color="inherit" onClick={()=>console.log('open')}>
<MenuIcon/>
</IconButton>
</ToolBar>
</AppBar>
</div>
);
}
}
export default Header
From the tutorial said that I should rename the directory from #material-ui/core/Menu on line 4 to #material-ui/icons/Menu. But when I revert back to #material-ui/core/Menu, the code is running, the button works, but no icon appeared.
But, from the tutorial shows that the program is running, with the icons appeared on it.
It has a white hamburger button icon on the header
This is the error code when I try to change core/Menu to icons/Menu
This is the result when I changed back to core/Menu
EDIT: The package.json I have right now:
{ "name": "static-page", "version": "0.1.0", "private": true, "dependencies": {
"#babel/runtime": "7.0.0-beta.55",
"#material-ui/core": "^1.4.3",
"#material-ui/icons": "^1.1.0",
"react": "^16.7.0",
"react-dom": "^16.7.0",
"react-reveal": "^1.2.2",
"react-scripts": "2.1.3",
"react-scroll": "^1.7.9",
"react-slick": "^0.23.1" }, "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" ] }
Found a MUI issue which states the exact same problem. The solution is to upgrade to #material-ui/icons#1.1.1.
Since you have #material-ui/icons#^1.1.0 as a dependency, deleting node_modules and running npm install again should be sufficient.
Have a go and try:
npm add #babel/runtime
Or upgrade babel runtime:
"#babel/runtime": "7.0.0-beta.55"

react-bootstrap not working as expected

I am trying to use react-bootstrap to style a web page.
package.json
{
"name": "chrome-ui",
"version": "0.1.0",
"private": true,
"dependencies": {
"react": "^16.0.0",
"react-dom": "^16.0.0",
"react-scripts": "1.0.16"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
},
"devDependencies": {
"react-bootstrap": "^0.31.5"
}
}
App.js
import React from 'react';
import { Button } from 'react-bootstrap'
function CheckState(props){
function handleClick(e){
console.log(props.state)
}
return(
<Button bsStyle="primary" onClick={handleClick}>Click for state</Button>
)
}
However this is what I'm seeing on the web page when I refresh:
It doesn't look like the styling is being added. What am I doing wrong?
Add import "bootstrap/dist/css/bootstrap.css"; in your index.js
In your react application, open index.js file and add import 'bootstrap/dist/css/bootstrap.css';

Resources