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

后端 未结 10 1938
遥遥无期
遥遥无期 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 01:40

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

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

提交回复
热议问题