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

140
Visualizações
getting material-ui TextField value onsubmit

I want to get the value of TextField input and render the message conditionally. I tried this one, its working but this one is functioning dynamically because I used onChange. I want to achieve the same but using onSubmit on <Button> Is there anyway to do that?

import React from 'react';
import { Component } from 'react';
import Button from '@mui/material/Button';
import { TextField } from '@mui/material';
class App extends Component 
{ 
    state = { 
        myValue: null, 
    } 
 
    handleChange = (e) => this.setState({ 
        myValue: e.target.value 
    }) 
     
    render() { 
        return ( 
            <div>
            <TextField 
                value={this.state.myValue} 
                onSubmit={this.handleChange}
            />
            <button  >Get Weather</button>
            {this.state.myValue ? <p>value inputed </p>: <p>no input</p>}
            
            </div>

        ) 
    } 
} 


export default App;
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

you just need to you onkeydown instead onsubmit.

<TextField 
            value={this.state.myValue} 
            onKeyDown={this.handleChange}
        />

or use

<form onSubmit={handleChange }>
  <TextField 
                value={this.state.myValue} 
                onKeyDown={this.handleChange}
            />
<button type="submit">submit</button>
</form>
about 4 years ago · Juan Pablo Isaza Relatório

0

Using Refs is what you need. You can get the current value of your input by clicking a button and only then change the state.

Demo

import React, { createRef } from "react";
import { Component } from "react";
import { TextField } from "@mui/material";
class App extends Component {
  constructor(props) {
    super(props);
    this.textInput = createRef();
    this.state = {
      myValue: ""
    };
  }
  showRefContent = () => {
    this.setState({
      myValue: this.textInput.current.value
    });
  };

  handleChange = (e) =>
    this.setState({
      myValue: e.target.value
    });

  render() {
    return (
      <div>
        <TextField inputRef={this.textInput} />
        <button onClick={this.showRefContent}>Get Weather</button>
        <p>
          {this.state.myValue.length > 0
            ? `text:${this.state.myValue}`
            : "no text"}
        </p>
      </div>
    );
  }
}

export default App;

about 4 years ago · Juan Pablo Isaza 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