Answers for "react return action in a function"

0

react return action in a function

import React, {useState} from 'react';

// Create a callback in the probs, in this case we call it 'callback'
function newCallback(callback) {
  callback('This can be any value you want to return')
}

const Callback = () => {

const [callbackData, setCallbackData] = useState('')

// Do something with callback (in this case, we display it on screen)
function actionAferCallback (callback) {
  setCallbackData(callback)
}

// Function that asks for a callback from the newCallback function, then parses the value to actionAferCallback
function requestCallback() {
  newCallback(actionAferCallback)
}

  return(
    <div>
      {/* A button to activate callback */}
      <button onClick={() => {requestCallback()}}>Request Callback</button>
      {callbackData}
    </div>
  )
}

export default Callback;
Posted by: Guest on January-06-2021

Code answers related to "react return action in a function"

Code answers related to "Javascript"

Browse Popular Code Answers by Language