0

I am trying to print "Hello" onto the screen when the add button is clicked. But it is not showing. Thank you in advance for any answers you may have!

import React, { Component } from 'react';
import './App.css';
import './Login.jsx';
import './Navbar.jsx';

class MainPage extends Component {
    render() {
        return (
            <div>
                <div className="main-container">
                    <h1 style={{textDecoration:'underline'}}>Tasks</h1>
                    <div className="input-group mb-3">
                        <input type="text" id="task" className="form-control" placeholder="New Task"/>
                        <div id="name"></div>
                        <div className="input-group-append">
                            <button id="btn" className="btn btn-success" onClick={this.addTask}>Add</button>
                        </div>
                    </div>
                </div>
            </div>
        );
    }

    addTask = () => {
        return (
            <div>
                <h2>Hello</h2>
            </div>
        )
    }
}

export default MainPage;
4
  • I think removing '=' from the function definition would work Commented Aug 26, 2020 at 13:54
  • On addTask you return a component, what you expect to happen? Are you trying to use alert() Commented Aug 26, 2020 at 13:56
  • for any HTML to be displayed, it has to be written inside your render function. I think you should use state here Commented Aug 26, 2020 at 13:58
  • you need to use the state that save if your tilte is display or not like : isTitleDisplay : false, next inside your jsx (where you want to see the title) {isTitleDisplay && <h1>YOUR TITLE</h1>}, last your function trigger by your button () => setState({isTitleDisplay: true}) .. Commented Aug 26, 2020 at 14:04

3 Answers 3

2

Returning an HTML in an onClickEvent make no sense, where are the result going to be displayed?

I would manage it with a state, something like this

    class MainPage extends Component {
    
    this.state = {
            buttonPress:false
        }

        render() {
            return (
                <div>
                    <div className="main-container">
                        <h1 style={{textDecoration:'underline'}}>Tasks</h1>
                        <div className="input-group mb-3">
                            <input type="text" id="task" className="form-control" placeholder="New Task"/>
                            <div id="name"></div>
                            <div className="input-group-append">
                                 <button id="btn" className="btn btn-success" onClick={this.addTask}>Add</button>
                                 {{this.state.buttonPress? <h2>Hello</h2> : <span/>}}
                            </div>
                        </div>
                    </div>
                </div>
            );
        }
    
        addTask = () => {
            this.setState({buttonPress:true});
        }
    }
    
    export default MainPage;
Sign up to request clarification or add additional context in comments.

Comments

0

Where would the function return the "Hello" code?

import React, { Component } from 'react';
import './App.css';
import './Login.jsx';
import './Navbar.jsx';

class MainPage extends Component {
 state = {
    showMessage: false
  }
  addTask= () => {
   this.setState({showMessage: true});
  };
    render() {
        return (
            <div>
                <div className="main-container">
                    <h1 style={{textDecoration:'underline'}}>Tasks</h1>
                    <div className="input-group mb-3">
                        <input type="text" id="task" className="form-control" placeholder="New Task"/>
                        <div id="name"></div>
                        <div className="input-group-append">
                            <button id="btn" className="btn btn-success" onClick={this.addTask}>Add</button>
                            {this.state.showMessage && <div>
                              <h2>Hello</h2>
                            </div>}
                        </div>
                    </div>
                </div>
            </div>
        );
    }

}

export default MainPage;

Try this code instead, I created a state which tracks the visibility of the div. More information here:

How do I display text on button click in React.js

Comments

0

You should use state

import React, { Component } from 'react';

class App extends Component {

    constructor(props) {
        super(props)
        this.state = {testVarible: null}
    }

    render() {
        return (
            <div>
                <div className="main-container">
                    <h1 style={{textDecoration:'underline'}}>Tasks</h1>
                    <div className="input-group mb-3">
                        <input type="text" id="task" className="form-control" placeholder="New Task"/>
                        <div id="name"></div>
                        <div className="input-group-append">
                            <button id="btn" className="btn btn-success" onClick={this.addTask}>Add</button>
                        </div>
                    </div>
                </div>
                {this.state.testVarible}
            </div>

        );
    }

    addTask = () => {
        this.setState({testVarible: (
          <div>
            <h2>Hello</h2>
          </div>
        )});
    }
}

export default App;

Comments

Your Answer

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