A
A
Alex Ozerov2021-05-01 07:21:42
React
Alex Ozerov, 2021-05-01 07:21:42

How are global methods (functions) stored in React?

Given: React + Redux
Let's say there are n-number of application pages. The main page has a calculator that takes input and produces a result based on that data. Let's say that all the logic of the calculator is a function:

function calc(a,b,c){
    return (a * b + c) / 20
}

The function is universal, and following the principles of DRY, I intend to use this function on other pages for similar calculations. Since I have been in React for a little over 10 days, I want to ask how such methods are stored? That is, can I create, for example, a separate folder, create a valid method.js in it and store such methods in one file, and through props already pass methods to the necessary components? Or how else are such methods stored in applications?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir Korotenko, 2021-05-01
@alex_vma

Make out like this

function calc(a,b,c){
    return (a * b + c) / 20
}
export default calc;

connect like this
import calc from '../code/calc'

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question