Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

138
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!