Solucionado (ver solução)
Solucionado
(ver solução)
2
respostas

O preventDefault não funciona.

Tento usar o preventDefault e ele não impede que ao clicar no botão a página recarregue. Assim não consigo adicionar visualizar a nota ou até mesmo um console que possa aparecer.

import React, { Component } from "react";
import "./FormularioCadastro.css";

class FormularioCadastro extends Component{

    constructor(props){
        super(props);
        this.titulo="";
        this.texto="";
    }


    _handleMudancaTitulo(evento){
        this.titulo = evento.target.value;
        evento.stopPropagation();

    }

    _handleMudancaTexto(evento){
        this.texto = evento.target.value;
        evento.stopPropagation();


    }

    _criarNota(evento){
        evento.preventDefault();
        evento.stopPropagation();
        this.props.criarNota(this.titulo, this.texto);
      }


    render(){

        return(
            <form className="form-cadastro">
            <input type="text"
             placeholder="titulo" 
             className="form-cadastro_input"
             onChange={ this._handleMudancaTitulo.bind(this) }
             />
            <textarea 
             rows={10} placeholder="Escreva sua nota..."
              className="form-cadastro_input"
              onChange= { this._handleMudancaTexto.bind(this) }
              />
            <button 
            className="form-cadastro_input form-cadastro_submit"
            onSubmit= { this._criarNota }

            >Criar Nota</button>
          </form>

        );

    }
}

export default FormularioCadastro;
2 respostas
solução!

Creio que o erro esteja em você chamar a função onSubmit() no button e não no form.

Fazendo essa substituição da chamada da função seu código fica assim:

import React, { Component } from "react";
import "./FormularioCadastro.css";

class FormularioCadastro extends Component{

    constructor(props){
        super(props);
        this.titulo="";
        this.texto="";
    }


    _handleMudancaTitulo(evento){
        this.titulo = evento.target.value;
        evento.stopPropagation();

    }

    _handleMudancaTexto(evento){
        this.texto = evento.target.value;
        evento.stopPropagation();


    }

    _criarNota(evento){
        evento.preventDefault();
        evento.stopPropagation();
        this.props.criarNota(this.titulo, this.texto);
      }


    render(){

        return(
            <form className="form-cadastro" onSubmit({this._criarNota.bind(this)})>
            <input type="text"
             placeholder="titulo" 
             className="form-cadastro_input"
             onChange={ this._handleMudancaTitulo.bind(this) }
             />
            <textarea 
             rows={10} placeholder="Escreva sua nota..."
              className="form-cadastro_input"
              onChange= { this._handleMudancaTexto.bind(this) }
              />
            <button 
            className="form-cadastro_input form-cadastro_submit"
            >Criar Nota</button>
          </form>

        );

    }
}

export default FormularioCadastro;

Muito obrigada pela resposta. Consegui resolver.

Quer mergulhar em tecnologia e aprendizagem?

Receba a newsletter que o nosso CEO escreve pessoalmente, com insights do mercado de trabalho, ciência e desenvolvimento de software