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

后端 未结 10 1908
遥遥无期
遥遥无期 2021-01-31 01:13

I create form, I have several TextField, DropDownMenu material-ui components included, question is how I can collect all data from all TextFields, DropDownMenus in one obj and s

10条回答
  •  时光说笑
    2021-01-31 02:00

    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 (
            
    ) }

提交回复
热议问题