16

Sorry if this topic it's probably a copy of another one, but i don't understand what i'm doing wrong with my code + i'm really new to react. I tried several solutions but none of them worked. I will put here some of the post that i read:

Problem

I need to console.log the string inside value with handleInput

Code

import React, {Component} from 'react';
import Button from './Button';
import Screen from './screen';
import './keyboard.css'

class NumberKeyboard extends Component {
    constructor(props){
        super(props)
        this.state = {
            operations: []
        }

    }

handleInput(props) {
    const buttonValue= this.props.value;
    console.log(buttonValue)
}


    render() {


        return (
            <div className="keyboard">
                <div className="column"></div>
                <div className="column">
                    <div className="keyboardRow roundBorder" value={"example"} onClick={() => this.handleInput('value')}>
                        <Screen className="crystalScreen"></Screen>
                        <Button value="clear" >C</Button> 
                        <Button value="±">±</Button>
                        <Button value=".">.</Button>
                        <Button value="">+</Button>
                    </div>
                    <div className="keyboardRow">
                        <Button value="clear">1</Button>
                        <Button value="2">2</Button>
                        <Button value="3">3</Button>
                        <Button value="-">-</Button>
                    </div>
                    <div className="keyboardRow">
                        <Button value="4">4</Button>
                        <Button value="5">5</Button>
                        <Button value="6">6</Button>
                        <Button value="*">X</Button>
                    </div>
                    <div className="keyboardRow lastRow">
                        <Button value="7">7</Button>
                        <Button value="8">8</Button>
                        <Button value="9">9</Button>
                        <Button value="%">÷</Button>
                    </div>
                </div>

                <div className="column"></div>
            </div>
        )
    }
}

export default NumberKeyboard;

i tried several attempts to solve it but every time the max result that i had it was the sadly undefined or an error.

--------------------------- UPDATE -------------------------

Due to visits of these post i want to give a little update and set an example based on hooks too:

import React, { useState } from 'react';
import KeyboardRow from 'src/components/keyboardRow'; /* ideally this
 should contain the buttons element. i'm writing in this way just to stay 
 closer to the initial question */  

function NumberKeyboard() {
  const [selectedNumber, setSelectedNumber] = useState(0);

  const selectNumber = numberSelected => {
       setSelectedNumber(numberSelected)
  }

  return (
  <>
    <KeyboardRow >
       <Button onClick={selectNumber} value="7">7</Button>
       <Button onClick={selectNumber} value="8">8</Button>
       <Button onClick={selectNumber} value="9">9</Button>
    </KeyboardRow >
    <div>{selectedNumber}<div>
  </>
  );
}
Legeo
  • 784
  • 4
  • 20
  • 44

4 Answers4

19

You are sending and receiving data in a wrong way. First, you need to use onClick={this.handleInput} or onClick={(e) => this.handleInput(e,'value')} instead of onClick={() => this.handleInput('value')} because you are sending 'value' string in function.

<div className="keyboardRow roundBorder" value={"example"} onClick={e => this.handleInput(e, "value")} >

And then receive in following ways:

handleInput(e) {
    console.log(e.target.value);
}

You ca check the working demo.

Triyugi Narayan Mani
  • 3,039
  • 8
  • 36
  • 56
  • Can i ask a little thing ? Just to be sure that I really understand what you wrote ( because yeah, it's work ): onClick={e => this.handleInput(e, "value")} the " e " stand for the children button that i click ? – Legeo Nov 27 '18 at 11:33
  • 1
    e is the event that is generated based on your click for the current element – Irshad Babar May 02 '20 at 22:03
4

First of all, your buttons don't currently do anything when clicked, so what we're going to need to do is add an onClick to each of your buttons: <Button onClick={this.handleInput} value="clear">C</Button>.

This will pass the event to handleInput.

To get the value of the button clicked we can do:

handleInput(el) {
    console.log(el.target.value);
}
amazonic
  • 246
  • 1
  • 7
2

You could use the event object. Something like:

    handleInput = e => {
      const buttonValue = e.target.value;
      console.log(buttonValue);
      //some logic
}

And then you could add the method on the onClick event, passing the event object as well.

   onClick = {this.handleInput}
noa-dev
  • 3,561
  • 9
  • 34
  • 72
Ovi
  • 192
  • 1
  • 3
  • 13
2
  • In handleInput function you were passing the "props" but using using "this.props"
  • onClick should be there for every button
  • Should not pass the string 'value' rather do this "onClick={(e) => this.handleInput(e)}"
  • you can access with event.target.value
Mouleesh Guru
  • 55
  • 1
  • 10