-1

Hi I am new to React and js.

I am trying to write two text boxes such that the user will insert two numbers and when clicking Add button, he will get the result.

My suggestion was:

import React, { Component } from 'react';

export class Home extends Component {
displayName = Home.name

  render() {
    return (
        <div>
        <h1>Hello world! This is a new Web App</h1>

            <input type="text" name="" id="txt1" />
            +
            <input type="text" name="" id="txt1" />
            = <br/>
            <button onClick="add()">Add</button>

        </div>
      );
     }
    }

And I thought and a function add() that will do the following:

function add()
{
 var a = getElementById("txt1").value;
 var b = getElementById("txt2").value;
 alert(pardeInt(a)+parseInt(b));
}

If someone could edit my suggested code so it will work, that would be great.

Thanks in advance.

2
  • 1
    This looks like you haven't read the first thing about how to use event handlers and form controls in react. Try reactjs.org/docs/handling-events.html and reactjs.org/docs/forms.html Commented Oct 24, 2018 at 12:17
  • You are just using react to render HTML and use javascript. This has nothing to do with actual react. Start by reading more about react components please. Maybe follow some tutorials? Commented Oct 24, 2018 at 12:35

2 Answers 2

1

First you need to bind the function to your context. This could be achieved by

this.myFunction.bind()

or through arrow function (that bind function for you)

then you have to update the state (the render will be executed again when state change):

import React, { Component } from 'react';

export class Home extends Component {
  state = { 
    result: 0,
    val1: 0,
    val2: 0,
  }

  handleChangeOne = (event) => {
    this.setState({val1: event.target.value});
  }
  
  handleChangeTwo = (event) => {
    this.setState({val2: event.target.value});
  }

  add = () =>
  {
   this.setState({
      result: parseInt(this.state.val1)+parseInt(this.state.val2)
      });
  }

  render() {
    return (
        <div>
        <h1>Hello world! This is the result: {this.state.result}</h1>

            <input onChange={this.handleChangeOne} type="text" />
            +
            <input type="text" onChange={this.handleChangeTwo} />
            = <br/>
            <button onClick={this.add}>Add</button>

        </div>
      );
     }
    }

Avoid to access directly to the DOM and rely to local state, instead.

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

1 Comment

Thank you. Maybe you can assist me also here? stackoverflow.com/questions/52984757/…
0

Your code is just telling that you don't have React.js and React Native proper knowledge.

Definitely you should first take a look into React.js basis and THEN if you want to build mobile apps, the React-Native documentation:

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.