Почему метод возвращает [object Object]?
Есть метод checkWinner
который который срабатывает при клике на компонент <ButtonResult checkWinner={this.checkWinner}/>
. Данный метод находить находить наибольшее значение ключа в объекте this.state
и выводит в консоль.
Проблема заключается в том что выводит {"[object Object]":1}
, а вместо [object Object]
должен быть id
к примеру voute1
Main.js:
export default class Main extends React.Component {
constructor(props) {
super(props);
this.state = {
vote1: 0,
vote2: 0,
vote3: 0,
vote4: 0,
vote5: 0,
}
this.increaseVote = this.increaseVote.bind(this);
this.checkWinner = this.checkWinner.bind(this);
}
increaseVote(id) {
return this.setState(state => {
console.log({[id]: state[id]});
return {
[id]: state[id] + 1
}
})
}
checkWinner(id) {
console.log(typeof id)
console.log(id)
console.log(typeof{[id]: Math.max(...Object.values(this.state))})
console.log({[id]: Math.max(...Object.values(this.state))})
return this.setState(state => {
return JSON.stringify({
[id.toString()]: Math.max(...Object.values(state))
})
})
}
render() {
return (
<Wrapper>
<List>
<Item text="😜" id="vote1" increaseVote={this.increaseVote} state={this.state}/>
<Item text="😈" id="vote2" increaseVote={this.increaseVote} state={this.state}/>
<Item text="🚀" id="vote3" increaseVote={this.increaseVote} state={this.state}/>
<Item text="😎" id="vote4" increaseVote={this.increaseVote} state={this.state}/>
<Item text="😶" id="vote5" increaseVote={this.increaseVote} state={this.state}/>
</List>
<ButtonResult checkWinner={this.checkWinner}/>
<Winner/>
</Wrapper>
)
}
}
Winner.js:
export default class Winner extends React.Component {
render() {
return (
<div className="block-winner"></div>
)
}
}
Item.js:
export default class Item extends React.Component {
render() {
const { id, text, state, increaseVote } = this.props;
const onClick = () => increaseVote(id);
return (
<li>
<Button onClick={onClick} id={id} text={text}/>
<Count text={state[id]}/>
</li>
)
}
}
**ButtonResult.js: **
export default class ButtonResult extends React.Component {
render() {
const { checkWinner } = this.props;
return (
<button onClick={checkWinner} className="button-result">Result</button>
)
}
}
Button.js:
export default class Button extends React.Component {
render() {
const { id, text, onClick } = this.props;
return (
<button onClick={onClick} className="button" id={id}>{text}</button>
)
}
}
Count.js:
export default class Count extends React.Component {
render() {
const { text } = this.props;
return (
<p className="count-click">{text}</p>
)
}
}
List.js:
export default class List extends React.Component {
render() {
return (
<ul className="list-buttons" id="listButtons">{this.props.children}</ul>
)
}
}
Wrapper.js:
export default class Wrapper extends React.Component {
render() {
return (
<div className="wrapper">{this.props.children}</div>
)
}
}
Источник: Stack Overflow на русском