Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
32
rated 0 times [  37] [ 5]  / answers: 1 / hits: 55591  / 6 Years ago, tue, november 27, 2018, 12:00:00

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>
</>
);
}

More From » reactjs

 Answers
34

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.


[#53033] Thursday, November 22, 2018, 6 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
eddiejoshb

Total Points: 659
Total Questions: 105
Total Answers: 100

Location: Singapore
Member since Sat, Jul 25, 2020
4 Years ago
;