1

Why can I not write a function and call a function using button in react.js? In this code, I want click button, an alert pop up and say that survey has submitted. How can i do it ?

function finish(){
    alert('Survey Submitted! Thank you');    

}

function Task3(){


    return(
        <div className="head">
          <h1>Experiment</h1>
          <h3>Task 3</h3>
          <p>In Task 3, you will see the right answer of your guess. </p>
          <div className="video">
          <div> <MultiStep steps={steps}/> </div>

          </div>
         <button className="task3-finish" onSubmit = {finish} >Finish Thanks!</button>

          </div>

    )



}

export default Task3

3
  • Is there any error ? Commented Mar 1, 2020 at 3:41
  • 1
    onSubmit = {()=> finish()} Commented Mar 1, 2020 at 3:41
  • it does not work. Commented Mar 1, 2020 at 3:45

3 Answers 3

3

Did you mean to use onClick? because onSubmit should be used on the <form> element.

<form onSubmit = {() => finish() }> 
</form>

<button onClick = {() => finish() }> 
</button>
Sign up to request clarification or add additional context in comments.

Comments

0

function Task3(){

finish(){
    alert('Survey Submitted! Thank you');    

}

    return(
        <div className="head">
          <h1>Experiment</h1>
          <h3>Task 3</h3>
          <p>In Task 3, you will see the right answer of your guess. </p>
          <div className="video">
          <div> <MultiStep steps={steps}/> </div>

          </div>
         <button className="task3-finish" type="button" value="Submit" onClick= {this.finish} >Finish Thanks!</button>

          </div>);



}

export default Task3

I think this could help.

Comments

0

you need to pass that new function as a prop. then you can call the new function.

your functional component can look like this. (i commented some codes and changed onsubmit as onclik)

Task3.js

    import React from "react";

function Task3(props) {
  return (
    <div className="head">
      <h1>Experiment</h1>
      <h3>Task 3</h3>
      <p>In Task 3, you will see the right answer of your guess. </p>
      <div className="video">
        {/* <div>
          {" "}
          <MultiStep steps={steps} />{" "}
        </div> */}
      </div>
      <button className="task3-finish" onClick={props.callTask3Prop}>
        Finish Thanks!
      </button>
    </div>
  );
}

export default Task3;

App.js

import React, { Component } from "react";
import Task3 from "./Task3";

class App extends Component {
  callTask3() {
    alert("Task3 call...");
  }

  render() {
    return (
      <div className="App">
        <Task3 callTask3Prop={this.callTask3} />
      </div>
    );
  }
}

export default App;

i have added a working example here, example

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.