score:2
you'll have to do this:
- add a package.json file with { “name”: “folder_name” } in it to the folder you’d like to import from.
import thing from ‘folder_name/thing’
orimport thing from ‘./thing’
…except when going “up” in the folder structure.
import blah from ‘../../../../../blah’
in your react native project, chances are you keep your code in a single folder, such as “app”. if you have a directory named “app” this is what an absolute path might look like:
import thing from ‘awesomeapp/app/some/thing’
what sucks about this is literally all import statements (or require calls, if you’re still into that) would start with “awesomeapp/app/”, which is a lot to ask, when the alternative is simple to add a series of “../” (the key strokes are just so close together, it’s too easy).
to alleviate this pain point, you can simply add a package.json file inside the folder from where you want to import. in this case since all our code is under the “app” folder, we’d put the file here:
awesomeapp/app/package.json
then, add a “name” property to the json file with the folder name as its value (you can call it whatever you’d like, but really, that’ll just confuse people, including you in 6 months). the shorter, the better.
{ “name”: “app” }
now, you can import using that name as a reference.
import thing from ‘app/some/thing’
Source: stackoverflow.com
Related Query
- Can you import using absolute paths in React?
- Can you use es6 import alias syntax for React Components?
- How to use absolute path to import custom scss, when using react + webpack?
- Can you import an external CSS file in a React js file?
- How can you render a react component using dynamic name?
- Using if statement to determine if a Firebase User can access certain React Router paths
- How can I import multiple functions or components from a package using react lazy
- Absolute import using Webpack resolve.alias in a React app
- Im using React Router and I want one of my route links with path="/" to be the home page on reload.. How can you do that?
- How do you import Javascript file from node_modules into react using Vite?
- How can you return Redirect using React Router?
- Can access paths externally using react router on top of a asp.net project
- Can you import a function from a react component into a test file?
- How can I use absolute paths in my React styleguidist component library?
- Can you change the cursor using CSS in a React application?
- Can you force a React component to rerender without calling setState?
- How to import and export components using React + ES6 + webpack?
- Can you use React Native to create a desktop application?
- You should not use Route or withRouter() outside a Router when using react-router 4 and styled-component in react
- Display images in React using JSX without import
- Using JavaScript Axios/Fetch. Can you disable browser cache?
- Can I avoid forceUpdate() when using React with Backbone?
- Can you early return with React hooks?
- How can I test a change handler for a file-type input in React using Jest/Enzyme?
- How can I write an else if structure using React (JSX) - the ternary is not expressive enough
- Why do you need to import React multiple times in parent and child components?
- Can you pass down state to child components with React Hooks?
- Can I build 3D or 2D interactive games using React Native?
- How can remove console.log in the production build of a React application created using create-react-app?
- I'm getting error using laravel and react "Invalid DOM property `for`. Did you mean `htmlFor`"
More Query from same tag
- JS/React useState value not updating before function call?
- how to add JS file in Node Js
- enable hot reload for vite react project instead of page reload
- OnSubmit not called if file not chosen
- how to limit the number of results for autocomplete input / ant.design
- Dispatch an action only once that too if an async action has successfully fetched data and updated the state
- ReactJS , How to change form type, from onChange dropdown
- ReactJS - rerender same component with different data?
- How do I compose redux connect and withCookies in my App?
- Using React Component in Angular 2
- Caching with react-query and react-router-dom
- Confused about Undefined Errors in React
- React custom synthetic event
- How can I get my Redux useSelector to update on store change?
- How to send parameter from anchor click event in typescript
- Ant design 4.0.1 - Access methods of child form inside class component from parent component
- How to output the cat_Id of a category sent by an api on clicking that category
- Reactjs Passing props down 2 components
- React guard change the route but does not show the component
- How to change the thumb icon of the material-ui slider
- React.js and jsx over ejs?
- React createRef returning current = null when creating dynamic generated components
- Make Navbar stick at the top but when clicked on cross button in notification bar it should be removed and navbar stick to top:0
- How to pass a variable inside a component in react
- React useReducer async data fetch
- How to use react router to use Hash in URLs
- how to get axios.post(`api/ImageUpload`, img_mode) value from $request in laravel controller
- What's the best way to deal with an error about an unmounted component? Trying to implement CKEditor but get this error
- How to fetch a value of key from localStorage and put it to input by hooks?
- Add to Homescreen React PWA