1

how when loading a page to click on a button in react?

I need a button to be pressed when the page loads

https://codesandbox.io/s/gifted-poitras-3sknp

import ReactDOM from "react-dom";

import "./styles.css";

function App() {
  return (
    <div className="App">
      <button onClick={() => alert("loaded")}>button</button>
    </div>
  );
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
2
  • Do you mean you want to alert upon page load? Commented Aug 23, 2019 at 4:19
  • yes, like onLoad method Commented Aug 23, 2019 at 4:20

3 Answers 3

2

Are you looking for something like this. Button clicks happens on page load and also when clicked on button?

class App extends React.Component {
  constructor(){
    super();
    this.buttonClicked = this.buttonClicked.bind(this);
  }
  
  componentDidMount(){
    this.buttonClicked();
  }
  
  buttonClicked(){
    alert("I'm Clicked");
  }

  render() {
    return (
      <button onClick={() => this.buttonClicked()}>
        button
      </button>
    );
  }
}

ReactDOM.render(<App />, document.getElementById("root"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<div id="root"></div>

Sign up to request clarification or add additional context in comments.

Comments

1

use useRef to save a reference to the button element combined with useEffect to detect when the component mounts

import React, { useEffect, useRef } from "react";

function App() {
  const buttonRef = useRef(null);

  useEffect(() => {
    buttonRef.current.click();
  }, []);

  return (
    <div className="App">
      <button ref={buttonRef} onClick={() => alert("button")}>
        button
      </button>
    </div>
  );
}

2 Comments

I need her to press a button when loading a page
@recile like this ?
1

From React's Hooks API Reference

The function passed to useEffect will run after the render is committed to the screen.

So you can always consider to use useEffect to run whatever side effects do you want right after the page rendered. Make sure to pass [] as the second argument to make sure the arrow function will only be called once.

This is an alternative example of using the useEffect hook with document.getElementById(id) instead of useRef since that has already been mentioned

It is still better to use useRef especially if the component will be reusable in the same page.

import React, {useEffect} from "react";

useEffect(() => {
  document.getElementById("btn").click();
},[]);

function App() {
  return (
    <div className="App">
      <button id="btn" onClick={() => alert("loaded")}>button</button>
    </div>
  );
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.