score:0
in order to deal with this situation, we can use some third-party components. the first library is the babel-plugin-root-import, which allows us to import components using root-based paths.
install
npm install babel-plugin-root-import --save-dev
custom root
const rootimportopts = {
root: __dirname,
rootpathprefix: '~/',
rootpathsuffix: 'src/js',
};
module.exports = {
plugins: [['babel-plugin-root-import', rootimportopts]],
};
babel.config.js
const rootimportopts = {
root: __dirname,
rootpathprefix: '~/',
rootpathsuffix: 'src/assets',
};
module.exports = (api) => {
api.cache(true);
const plugins = [['babel-plugin-root-import', rootimportopts]];
return { plugins };
};
now you can use like this:
<img src={require(`~/your-image-name`)} alt=""/>
for more information you can visit babel-plugin-root-import github.
- this second we need to install is the react-app-rewired. this component allow us to change webpack configuration without using ‘eject’ and without creating a fork of the react-scripts
Source: stackoverflow.com
Related Query
- How can I reference photos in a folder locally in react?
- How can I define the type for a <video> reference in React using Typescript?
- How can I require react components from a local folder without node_modules from a globally installed node app?
- How can I list source folder contents in React or create-react-app?
- How do i have an assets file that i can use in any file in my react app without always having to navigate to the assets folder
- How can i download csv file and store it on public folder in react js
- How can I locally access my react js app with wamp and allow access to other computers?
- How can I display photos when I click dropdown items with React
- How can I communicate between related react components?
- How can I prevent event bubbling in nested React components on click?
- How can one tell the version of React running at runtime in the browser?
- How to create a React app directly in the current folder
- How can I make use of Error boundaries in functional React components?
- How can I reset a react component including all transitively reachable state?
- react Material-ui, How do I know I can use onClick for button?
- How can I render repeating React elements?
- How can I use React hooks in React classic `class` component?
- How can I render HTML from another file in a React component?
- React: How much can I manipulate the DOM React has rendered?
- How can I block a React component to be rendered until I fetched all informations?
- How can I get input radio elements to horizontally align in react [material-ui]?
- How can I remove an attribute from a React component's state object
- How can I prevent my functional component from re-rendering with React memo or React hooks?
- How do you reference a process.env variable in HTML <script src="" ? React
- How can I parse through local JSON file in React js?
- How can I call parent method in a child React component?
- How can I test a change handler for a file-type input in React using Jest/Enzyme?
- How can I add multiple className's to react component?
- How can one extend React types to support html attributes as props?
- How can I write an else if structure using React (JSX) - the ternary is not expressive enough
More Query from same tag
- Is this the correct way for storing a sub-collection?
- loading status change after fetch data completely
- Pass in logic in react on rails props
- React DID NOT update components after update list state (by hook)
- Enzyme Shallow not finding a react-bootstrap Component
- Is there a way to use custom icon for material ui's pagination
- ReactJS Using styles within classComponent
- Working with DOM elements in react testing-library?
- Problem creating a table with dynamic column cells
- Why the variable in styled-jsx tag didn't work?
- Render ES6 component is not working in codepen
- How to avoid re-renders in React when using conditional Render and children node prop
- How do I get React to work with Babel and Gulp?
- How to read a cookie with react-cookie?
- Conditional CSS Reactjs in Tab Navigation
- onSubmit button not redirecting to Profile component page
- Table in Reactjs Data Structures
- How to validate CreatableSelect component (multi select) with yup (and react-form-hooks)
- How to center an element related to it's above sibling
- Multiple If conditions in JSX
- How to change ant-design Tabs default border color
- Handle outside click closes on clicking the modal itself. Basically shouldn't close when clicked anywhere but outside of the modal
- Express / NextJS - Multiple directories in a single express route
- Regular expression for syntax highlighting attributes in HTML tag
- Axios is not placing the origin header in the request
- React rendering variable with html characters escaped
- How to check which element is hovered using React 17.0.2
- React Material ui FilledInput cannot focus input when change state with onFocus event
- Specify gulp-inject Script Type
- clear controlled components in a form using react after clicking submit button