0
I’m starting with React and can not understand why the code below does not work. I receive the following message: "Typeerror: can’t access Property "aoClicarNota", this is Undefined"
I’m already using aoClicarNota as Arrow Function but it still doesn’t work. What am I doing wrong?
import React from 'react';
import axios from 'axios';
import { Button, ButtonGroup, Container } from 'react-bootstrap';
class BlocoNotasPossiveis extends React.Component {
constructor(props) {
super(props);
this.state = {
notasPossiveis: [
{
nota: '1',
descricaoCurta: 'Nota baixa',
descricaoLonga:
'Serviços sempre apresentam falhas, com grande impacto na produtividade, gerando retrabalho constantemente.'
},
{
nota: '2',
descricaoCurta: 'Nota ruim',
descricaoLonga:
'Serviços sempre apresentam falhas, com grande impacto na produtividade, gerando retrabalho constantemente.'
}
]
};
}
render() {
return (
<div className="sombra">
<ButtonGroup aria-label="First group">
{this.state.notasPossiveis.map(this.renderizarBotao)}
</ButtonGroup>
</div>
);
}
aoClicarNota = () => {
console.log("valor.nota")
}
renderizarBotao(valor) {
return (
<Button variant="primary" size="lg" onClick={this.aoClicarNota}>
<span className="linhaNumeral">{valor.nota}</span>
<span className="linhaDescricaoCurta">{valor.descricaoCurta}</span>
</Button>
);
}
}
export default BlocoNotasPossiveis;
When you use class component in React, you need to bind the function within the constructor. In your case, try to place
this.aoClicarNota = this.aoClicarNota .bind(this);
and see if it works.– Colasanto
@Colasanto Unfortunately it didn’t work by making the bind in the builder. grateful.
– Davis