score:251
quick note: you are importing a class, you can't call properties on a class unless they are static properties. read more about classes here: https://developer.mozilla.org/en-us/docs/web/javascript/reference/classes
there's an easy way to do this, though. if you are making helper functions, you should instead make a file that exports functions like this:
export function hellochandu() {
}
export function hellotester() {
}
then import them like so:
import { hellochandu } from './helpers'
or...
import functions from './helpers'
then
functions.hellochandu
score:0
create a file with name e.g utils.js and use export with all functions.
export function firstfunction(){
}
export function secondfunction(){
}
now there are two ways you can import and use these functions
- import them separately as
import {firstfunction, secondfunction} from './utils'
and use them as
firstfunction()
secondfunction()
- import them by giving generic name as
import * as utils from './utils'
and use them as
utils.firstfunction()
utils.secondfunction()
score:2
i prefer to create folder his name is utils and inside create page index that contain what that think you helper by
const findbyattr = (component,attr) => {
const wrapper=component.find(`[data-test='${attr}']`);
return wrapper;
}
const function_name = (component,attr) => {
const wrapper=component.find(`[data-test='${attr}']`);
return wrapper;
}
export {findbyattr, function_name}
when you need to use this it should be imported as use "{}" because you did not use the default keyword look
import {function_name,findbyattr} from'.whare file is store/utils/index'
score:3
if you want to use class, you can do this.
helper.js
function x(){}
function y(){}
export default class helper{
static x(){ x(); }
static y(){ y(); }
}
app.js
import helper from 'helper.js';
/****/
helper.x
score:29
to achieve what you want and have a better organisation through your files, you can create a index.js to export your helper files.
let's say you have a folder called /helpers. inside this folder you can create your functions divided by content, actions, or anything you like.
example:
/* utils.js */
/* this file contains functions you can use anywhere in your application */
function formatname(label) {
// your logic
}
function formatdate(date) {
// your logic
}
// now you have to export each function you want
export {
formatname,
formatdate,
};
let's create another file which has functions to help you with tables:
/* table.js */
/* table file contains functions to help you when working with tables */
function getcolumnsfromdata(data) {
// your logic
}
function formatcell(data) {
// your logic
}
// export each function
export {
getcolumnsfromdata,
formatcell,
};
now the trick is to have a index.js inside the helpers folder:
/* index.js */
/* inside this file you will import your other helper files */
// import each file using the * notation
// this will import automatically every function exported by these files
import * as utils from './utils.js';
import * as table from './table.js';
// export again
export {
utils,
table,
};
now you can import then separately to use each function:
import { table, utils } from 'helpers';
const columns = table.getcolumnsfromdata(data);
table.formatcell(cell);
const myname = utils.formatname(somenamevariable);
hope it can help to organise your files in a better way.
score:30
i am sure this can help. create filea anywhere in the directory and export all the functions.
export const func1=()=>{
// do stuff
}
export const func2=()=>{
// do stuff
}
export const func3=()=>{
// do stuff
}
export const func4=()=>{
// do stuff
}
export const func5=()=>{
// do stuff
}
here, in your react component class, you can simply write one import statement.
import react from 'react';
import {func1,func2,func3} from 'path_to_filea';
class htmlcomponents extends react.component {
constructor(props){
super(props);
this.rippleclickfunction=this.rippleclickfunction.bind(this);
}
rippleclickfunction(){
//do stuff.
// foo==bar
func1(data);
func2(data)
}
render() {
return (
<article>
<h1>react components</h1>
<ripplebutton onclick={this.rippleclickfunction}/>
</article>
);
}
}
export default htmlcomponents;
score:100
an alternative is to create a helper file where you have a const object with functions as properties of the object. this way you only export and import one object.
helpers.js
const helpers = {
helper1: function(){
},
helper2: function(param1){
},
helper3: function(param1, param2){
}
}
export default helpers;
then, import like this:
import helpers from './helpers';
and use like this:
helpers.helper1();
helpers.helper2('value1');
helpers.helper3('value1', 'value2');
Source: stackoverflow.com
Related Query
- How to create helper file full of functions in react native?
- How to create text file in React native (expo)?
- React Native - how to use local SVG file (and color it)
- react native how to call multiple functions when onPress is clicked
- How to create two columns with space beetwen in react native - flatList
- how to create common helper class in React JS using ES6 which is used by another component?
- How to create custom React Native components with child nodes
- How to create a custom top navigation bar in React Native
- How to import a file into a react app that uses create react app as raw text?
- Create react app - how to copy pdf.worker.js file from pdfjs-dist/build to your project's output folder?
- How to be able to select file when using React Native Web?
- How to create a debug apk in react native expo
- how to set react native button to full width
- How to create multiple react native apps with same functionality
- React native and MobX: How to create a global store?
- How can i dispatch an action from a helper function in react native
- react js how to import only the required function from a file and not the all functions
- How to play a segment of a Lottie Animation (.json file ) in React Native
- React Native - How to prevent full screen video mode in IOS?
- How to create react components which share functions but have independent state?
- REACT: How to create a reusable custom hook with functions and effects that can change & react to changes in a host component's data
- How can I create a configuration file for react and prevent webpack bundling it?
- How to use props in helper functions in React
- How to import minified js file on react create app
- How to create a global settings file in react css modules
- how to change state value in helper function file in react js
- How can I exclude a source named test.js from being detected to be test file in a create react app?
- How to create _redirects file for React JS app to deploy it on Netlify
- How to create an excel file in react js with custom headings and styles while doing export to excel?
- I have trouble looking for a solution on where and how to create property file in React JS
More Query from same tag
- Get HTML in JSON to render as HTML in React Component
- React and Redux: Pulling Data From Payload of Action Type
- Store x and y coordinates of mouse movements in a text file
- How to properly render heavy web socket stream in React?
- Passing id from this.props.data.id to React ReduxForm
- React Router V4 - Route between components and not entire page
- Using Javascript to class manipulation in ReactJs
- asynchron firestore query with mapStateToProps
- React app throws TypeError: Cannot read properties of undefined (reading 'name') when compiling
- How to avoid saving wrong input data in react?
- Learner Question: How would I associate a button within a message bar (Fabric UI) with a function?
- Uncaught Error: useNavigate() may be used only in the context of a <Router> component in cypress unit testcases
- how to check file type in multer
- Implementing multiple routing in React
- How to map object properties
- How to get React onClick to work? (Doesn't work even with arrow function)
- How to get last month day 15 and current month day 16 in moment
- OnClick listener for icon
- React router not working with params
- React Function call not working when mapped in JSX
- How to redirect in react if prop is undefined
- React - Styled Component change with Click
- Spring boot + (Thymeleaf is overriding React JS views)
- How can I align my labels, input, and textarea on same line?
- TypeScript: Type of property incompatible when assigning from generic type
- getting Cannot read property getDate of undefined error
- How to hide text when a div translates over it on hover?
- How to add external JS on index.html in React boilerplate?
- How to change the URL and content without refresh website using reactJS?
- Concat array via checkboxes in Reactjs