How do i access a function in a component with a button in a different component in React?

Burak

I am using react-timer-hook to set up a stopwatch for my React Application. So I got a Timer.js file where this stopwatch is working with a start and pause button. I am calling the component in App.js so the timer shows up on all pages. My next step is to set up that start functionality in a different component named Start.js (and later on the pause in the final page), but I can't figure out how I can pass it to this component.

This is my App.js:

function App() {
  return (
    <Router>
      <div className="App">
        <Timer/>
        <Routes>
            <Route path="/" element={ <Start /> }></Route>
            // Other routes
        </Routes>
      </div>
    </Router>
  );
}

export default App;

Timer.js file:

function MyTimer({ expiryTimestamp }) {
  const {
    seconds,
    minutes,
    start,
    pause,
  } = useStopwatch({ expiryTimestamp, onExpire: () => console.warn('onExpire called') });


  return (
    <div className="timer">
      <div className='timer-inside' >
        <span>{minutes < '10' ? '0' + minutes : minutes}</span>:<span>{seconds < '10' ? '0' + seconds : seconds}</span>
      </div>
      <button onClick={start}>Start</button>
      <button onClick={pause}>Pause</button>
    </div>
  );
}

export default function App() {
  return (
    <div>
      <MyTimer/>
    </div>
  );
}

So basically i want the <button onClick={start}>Start</button> snippet to work in a file called Start.js, instead of the Timer.js itself.

Ayoze Barrera

If you want to render the Start component as a children of Timer, you can pass the start function received from the useStopwatch hook like

<Start onClick={start} />

And inside the Start

<button onClick={onClick}>Start</button>

But, if you want to render it on App, you need to call the hook on App and pass what you need on the components you need:

function App() {
  const timer = useStopwatch({ onExpire: () => console.warn('onExpire called') });

  return (
    <Router>
      <div className="App">
        <Timer data={timer} />
        <Routes>
            <Route path="/" element={ <Start data={timer} /> } />
            // Other routes
        </Routes>
      </div>
    </Router>
  );
}

export default App;

So in both components will have same data property.

Hope it helps!

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I bind a button rendered by a React component to a function in that component?

How do I refresh a react component by clicking on a button outside the component?

React: How do I access a component from another component?

How do I make a button act like a submit button for a form when it's in a different component in react?

how do I use "this" on an component function in react?

how do i pass props a react bootstrap button onClick function to anther component

How do I access a ref in a functional child component that is wrapped in React-Redux's connect function?

How to make a button onClick in one component call a function in a different component React

How can I render different component using onClick() function in React?

How do I add React component on button click?

How do I render a component when a button is clicked in React Native

How do I solve an error in my React Button Component?

how do i reuse a react component but with different colours?

How do i access redux state from another react component?

Access function in react Component

How can I use the function in Parent component when a button is clicked in child component react hooks?

How do I return a react component inline from a function?

How do I turn a JSX Element into a Function Component in React?

How do I pass a function as a property to a React component?

How do I rewrite a react component with decorators as a pure function?

How do I change the component from inside a map function in react?

How do I add a onclick function to a custom React Component?

How do I change state of another component from different component in react native?

Detect if a button is pressed in a different component to run a function in another function react

How to access state from a react function component?

How can I call a component with a react button?

How do you change state of a react component from a different component?

How do I add a button component in ReactJS?

How do I add a button into a stateless component?