Javascript return values

I have a helper which returns the result of a function:

        TimerCalc =()=>{    
           (...other calculations...)
           const formatedTime = () => {
           return [pad(parseInt(seconds / 60)), pad(seconds % 60)].join(':')
           }
    
        return formatedTime()
        }

In the parent component I recieve that value like this:

  const counter = <TimerCalc resetTimer={reset} runTimer={startStopTimer} />

...which correctly returns the formated Time in the desired format like "00:00"

But I need now to return a second value together with the formatedTime, so I am trying:

return [formatedTime(), secondValue]

Hoping that something like counter[0] would give me the first value and conter[1] the second in the parent component. But it does not.

The problem is that I do not know how to get both values, because doing console.log({counter}) shows me an object, without values:

Object {
  "counter": Object {
    "$$typeof": Symbol(react.element),
    "_owner": FiberNode {
      "tag": 0,
      "key": null,
      "type": [Function Today],
    },
    "_store": Object {},
    "key": null,
    "props": Object {
      "resetTimer": false,
      "runTimer": false,
    },
    "ref": null,
    "type": [Function TimerCalc],
  },
}

Can anyone help, telling me how to do this right and access both values in the parent component? Thx!

Upvotes: 1

Views: 61

Answers (1)

Ali Sattarzadeh
Ali Sattarzadeh

Reputation: 3549

it seems that you are using react, so you cannot get returned value of component in this way , actually we use custom hook for this kind of case

like this :

const useTimerCalc =({resetTimer,startStopTimer})=>{
  (...other calculations...)
  const formatedTime = () => {
    return [pad(parseInt(seconds / 60)), pad(seconds % 60)].join(':')
  }

  return [formatedTime(), secondValue]
}

and in other component use it in this way :

  const [formatedTime,secondValue] = useTimerCalc({
    resetTimer:reset,
    startStopTimer:startStopTimer
  })

Upvotes: 2

Related Questions