Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

341
Visualizações
Uncaught (in promise) ReferenceError: <function> is not defined

I'm really new to ReactJs and currently struggling to create an onClick event, that will take the href value of itself and pass it to a function called 'action'.

The action function should then prevent the default browser behavior, perform a GET request to the passed in URL, and show an alert dialog containing the response and status.

However I am stuck trying to call my function from the onClick event with the following error:

Uncaught (in promise) ReferenceError: action is not defined

var Content = React.createClass({
    getInitialState: function() {
        return {
            teams: []
        }
    },

    componentDidMount: function() {
        const makeRequest = () => axios.get("http://localhost:5000/api/teams").then(({ data }) => this.setState({ teams: data }));

        this.serverRequest = makeRequest();

        this.poll = setInterval(() => {
            this.serverRequest = makeRequest();
        }, 10000)
    },

    componentWillUnmount: function() {
        this.serverRequest.abort();

        clearInterval(this.poll)
    },

    action: function(e) {
        e.preventDefault();

        $.get(e.href, function(res, status) {
            alert("Response: " + res + "\nStatus: " + status);
        });
    },

    render: function() {
        const { teams } = this.state;

        return (
            <div className="list-group">
                { teams.map(function(team) {
                    return ([
                        <a href={ "http://localhost:5000/api/teams?name=" + team.name} onClick={ action }>Click Me</a>
                    ]);
                })}
            </div>
        )
    }
});

ReactDOM.render(<Content />, document.getElementById('content'));
over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

Try this.action instead of action.

UPDATE

I found the problem. It is all about scope. You can't access this inside map.

render: function() {
    const { teams } = this.state;

    return (
        <div className="list-group">
            { teams.map((team) => {
                return ([
                    <a href={ "http://localhost:5000/api/teams?name=" + team.name} onClick={ this.action }>Click Me</a>
                ]);
            })}
        </div>
    )
}
over 4 years ago · Santiago Trujillo Relatório

0

Its a binding issue, try this it will work:

render: function() {
    const { teams } = this.state;

    return (
        <div className="list-group">
            { teams.map((team,i)=>{
                    return <a key={i} href={ "http://localhost:5000/api/teams?name=" + team.name} onClick={ this.action }>Click Me</a>
                })
            }
        </div>
    )
}

Suggestion: Whenever creating html elements dynamically, always assign the unique key to each element, key value will keep your components uniquely identified.

From Facebook React Doc:

Keys help React identify which items have changed, are added, or are removed. Keys should be given to the elements inside the array to give the elements a stable identity.

over 4 years ago · Santiago Trujillo Relatório

0

In my case, I forgot to require the function. So my suggestion is, please do check the function is imported correctly.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda