Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React read value of button clicked

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:

  • React js onClick can't pass value to method

  • How do I get the id of a button that was clicked? ReactJS

  • How to pass tag id to to onClick function in react?

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>
  </>
  );
}
like image 595
Legeo Avatar asked Nov 27 '18 11:11

Legeo


3 Answers

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.

like image 50
Triyugi Narayan Mani Avatar answered Nov 11 '22 19:11

Triyugi Narayan Mani


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);
}
like image 27
amazonic Avatar answered Nov 11 '22 18:11

amazonic


  • 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
like image 38
Mouleesh Guru Avatar answered Nov 11 '22 19:11

Mouleesh Guru