How get data from material-ui TextField, DropDownMenu components? How get data from material-ui TextField, DropDownMenu components? javascript javascript

How get data from material-ui TextField, DropDownMenu components?


Add an onChange handler to each of your TextField and DropDownMenu elements. When it is called, save the new value of these inputs in the state of your Content component. In render, retrieve these values from state and pass them as the value prop. See Controlled Components.

var Content = React.createClass({    getInitialState: function() {        return {            textFieldValue: ''        };    },    _handleTextFieldChange: function(e) {        this.setState({            textFieldValue: e.target.value        });    },    render: function() {        return (            <div>                <TextField value={this.state.textFieldValue} onChange={this._handleTextFieldChange} />            </div>        )    }});

Now all you have to do in your _handleClick method is retrieve the values of all your inputs from this.state and send them to the server.

You can also use the React.addons.LinkedStateMixin to make this process easier. See Two-Way Binding Helpers. The previous code becomes:

var Content = React.createClass({    mixins: [React.addons.LinkedStateMixin],    getInitialState: function() {        return {            textFieldValue: ''        };    },    render: function() {        return (            <div>                <TextField valueLink={this.linkState('textFieldValue')} />            </div>        )    }});


use the accepted answer / this was the answer to another (already deleted) question

@karopastal

add a ref attribute to your <TextField /> component and call getValue() on it, like this:

Component:

<TextField ref="myField" />

Using getValue:

this.refs.myField.getValue()


Here all solutions are based on Class Component, but i guess most of the people who learned React recently (like me), at this time using functional Component.So here is the solution based on functional component.

Using useRef hooks of ReactJs and inputRef property of TextField.

    import React, { useRef, Component } from 'react'    import { TextField, Button } from '@material-ui/core'    import SendIcon from '@material-ui/icons/Send'    export default function MultilineTextFields() {    const valueRef = useRef('') //creating a refernce for TextField Component    const sendValue = () => {        return console.log(valueRef.current.value) //on clicking button accesing current value of TextField and outputing it to console     }    return (        <form noValidate autoComplete='off'>        <div>            <TextField            id='outlined-textarea'            label='Content'            placeholder='Write your thoughts'            multiline            variant='outlined'            rows={20}            inputRef={valueRef}   //connecting inputRef property of TextField to the valueRef            />            <Button            variant='contained'            color='primary'            size='small'            endIcon={<SendIcon />}            onClick={sendValue}            >            Send            </Button>        </div>        </form>    )    }