I have a question. As in Angular JS we can create a global service file and we can inherit each and every component which obviously allows us to use the functions; written in the global services file, in the respective component.
Do we have the same option in React JS?
CodePudding user response:
Export function as named exports and import as many of them where ever you want. Example code- create a js file and define as many named export function like below
export const func1=()=>{//function 1}
export const func2=()=>{//function 2}
export const func3=()=>{//function 3}
export const func4=()=>{//function 4}
import as many of them on any component-
import {func1,func3} from './custom'
CodePudding user response:
one option that works for me is to make a file/module named like "global.js" where you can store global variables and functions
const global = {
variable_1 = '',
...
function_1 = () => { }
...
}
module.exports = {
variable_1: variable_1,
function_1: function_1,
...
}
and in the file where you need your global variables you have to import them
const global = require('./<path-to-global>')