React Hook: how to use "#foo/bar" to import custom hooks? - reactjs

I have a custom React Hooks, named bar at foo directory, and I import it with the way below:
import * from "../../foo/bar";
and the question is, how can I do it with the way:
import * from "#foo/bar";
to avoid find the directory boring.

Related

How to use a variable for import video in React?

We have to import audio in react like this example:
import audio from './audio1.mp3';
But what can I do to use different path which I have in my object. How can I change audio1.mp3 path to audio2.mp3?

import React from 'react'

When we write code in index.js file in src folder of an React app first of all we write this line:
import React from 'react';
I know react is a package
But I want to know what is React basically
an object, a method or something else.
The React variable that you import is an object, and it contains most of the methods that are used by React when generating a web-page.
The reason this import has been historically required is because the JSX that you write (e.g. return <p>text</p>) gets converted into a function call, calling the React.createElement function.
Note that in newer versions of React you no longer need to import react when using JSX. See https://reactjs.org/blog/2020/09/22/introducing-the-new-jsx-transform.html for more information.
Credit to Olivier Boissé for an answer in the comments.
In order to find out what React is, you just need to write:
console.log(react);
Then you will see that it is an object with many properties and methods.
String:
import React from "react";
We are writing in order to import this object into a file where we will use some of its method. If you don't use anything from React in the file, then you don't need to import it.
For example, in react 18, it is no longer necessary to import the react object into a file index.js
import { createRoot } from 'react-dom/client';
const root = createRoot(document.getElementById('root'));
root.render(<App />);
In any case, at this stage of your study of react, it may not be entirely clear to you what is used in it and for what, but in the future you will become more aware of all the possibilities of react. There is a time for everything

React export components based on folder structure

I created a private repo so I can reuse it in my projects. In my frontend app, I want to import the components based on their specific folder structure. Basically this my structure in my reusable repo:
- components
- dialogs
- ResponseDialog.js
- ConfirmationDialog.js
- form
- BaseTextField.js
- BaseSelect.js
- helpers
- date.js
- string.js
In my frontend I want to import it like this:
import { BaseTextField } from '#my-repo/components'
import { date } from '#my-repo/helpers'
I also want to include material ui there and I want to import it like this:
import { Button } from '#my-repo/mui/core'
instead of
import { Button } from '#mui/core';
I want this structure so I don't need to import all the material ui related repo every single time and also my components are just material ui components with modifications
You need to learn about jsconfig.json file.
Here is the reference

Import SVG vs. Require SVG

I'm working on a React project, and creating a component called SVGLogo that simply imports an svg and exports as a component. I noticed that when I use require() to import my svg file, it works fine:
const SVGLogo = require('../../../../../vft-site/src/images/logo.svg');
But my linter suggests I use the import statement. I changed it but now I get the error 'cannot find module... or its corresponding type declarations':
import SVGLogo from '../../../../../vft-site/src/images/logo.svg';
Why are these different?
import comes in es6 like a new feature and require can be called from anywhere but import can be called on top of script

React + Redux :import from multiple files with the same statement?

Would it be possible to import * from multiple files within the same import declaration i.e
Imports would usually be made with one file as follows
import * as action from '../actions/ActionCreators'
Would the following be possible by some means?
import * as action from { '../firstActions/FirstActionCreators', '../secondActions/SecondActionCreators' }
whereby the same is import is made from two different directories/files.
Thanks
The best practice is to import only the required stuff. Since you are using ES6 you can try like this.
import { Router, Route, Link, browserHistory,IndexRoute } from 'react-router'

Resources