react update screen after changing values in the state

Satoshi Nakamoto

I am new to react and I am working on a project where I was ask to reset a form to its defaults.

I created a function that gets call after I click the reset button

<input id="reset_button"
       type="button"
       name="reset"
       value="Reset"
       onClick={this.resetSearch}/>

This is my function:

resetSearch: function() {
  this.setState({ID: 'Moo'});
},

I do see the ID change value in the console but it does not update on the screen.

Other things that I have tried

  # when I do this the element despairs from then screen 
  resetSearch: function() {
   var values = this.fields.state.values;
     this.setState({
       defaultValues: {
         values
       },
      ignoreDefault: false
   });
  }

  #render function
  render: function() {
    return (
      <div className="card-body with-padding-bottom-0">
        <form id={this.formId}>
          <div id="sn-fields" className="usa-grid-full sn-search">
            <SNFields ref={(fields) => { this.fields = fields; }} ddl_id='sn_search_card_type' snOptions={ this.getProp('snOptions')} fields={this.getProp('fields')} updateParentState={this.updateStateByField} defaultFieldValues={this.getProp('defaultValues')} ignoreDefault={this.state.ignoreDefault}></SNFields>
          </div>

          <div className="usa-grid-full with-margin-top-10 validation-div">
            <div id="sn_search_card_search_button_container" className="usa-width-one-whole">
              <label htmlFor="system_validator"></label>
              <input hidden name="system_validator" id="system_validator"/>
              <input id="search_button" type="button" name="search" value="Search" onClick={this.personSearch}/>
              <input id="reset_button" type="button" name="reset" value="Reset" onClick={this.resetSearch}/>
            </div>
          </div>
        </form>
      </div>
    );
  }

I was able to find a class SNFields

var SNFields = React.createClass({

  filterFields: function(searchVal) {
    console.log('PCQSFields - filterFields ')
    var filterLabels = [];

    //filter in this component since the filtering can't be done on the ruby side
    switch(searchVal) {
      case 'APPLICATION_ID':
      case 'ENUMERATOR':
      case 'ENCOUNTER_ID': {
        filterLabels = ['ID'];
        break;
      }
      case 'NAME_AND_DOB': {
        filterLabels = ['Date of Birth', 'Last Name', 'Date Range', 'First Name'];
        break;
      }
      default: {
        break;
      }
    }

    var fields = this.props.fields.slice();
    for (var i = fields.length - 1; i > -1; i--) {
      if (filterLabels.indexOf(fields[i].label) < 0) {
        fields.splice(i, 1);
      }
    }

    return fields;
  },

  render: function() {
    console.log('NSFields - render ')
    return (
      <div>
        <div className="usa-width-one-third">
          <label htmlFor={this.props.ddl_id} className="card-label bold">Search Type</label>
          <Dropdown id={this.props.ddl_id} onChange={this.updateFields} selectableArray={this.props.nsOptions} classes="" selectedOption={this.state.ddl}/>
        </div>
        <div className="flex-container" style={{'flexWrap': 'row'}}>
          {this.nsFieldsHelper(this.state.fields)}
        </div>
      </div>
    );
  }

});

I guess what I really want to do is when I press the reset to call

SNFields.filterFields('NAME_AND_DOB') but when I try that I get a message in the console that reads: Uncaught TypeError: NSFields.filterFields is not a function

Gunnar Siréus

How does your componentDidMount() and componentWillReceiveProps(newProps) look like? This is how I have done an Input component:

import React, { Component } from 'react';
    export default class Input extends Component {
        displayName: 'Input';
        constructor(props) {
            super(props);
            this.state = {
                value: this.props.value,
                disabled: this.props.disabled,
                checked: this.props.checked,
                className:this.props.className,
                maxLength:this.props.maxLength,
                placeholder:this.props.placeholder,
                id:this.props.id,
                name:this.props.name,
                type:this.props.name,
                oldValue:this.props.value,
                backgroundColor:''
            };
            this.handleBlur = this.handleBlur.bind(this);
            this.handleChange = this.handleChange.bind(this);
        };
        componentWillReceiveProps(nextProps) {
            if (this.state.value !== nextProps.value) {
                this.setState({ value: nextProps.value});
            };
            if (this.state.disabled !== nextProps.disabled) {
                this.setState({ disabled: nextProps.disabled});
            };
            if (this.state.checked !== nextProps.checked) {
                this.setState({ checked: nextProps.checked});
            };
            if (this.state.className !== nextProps.className) {
                this.setState({ className: nextProps.className});
            };
            if (this.state.maxLength !== nextProps.maxLength) {
                this.setState({ maxLength: nextProps.maxLength});
            };
            if (this.state.placeholder !== nextProps.placeholder) {
                this.setState({ placeholder: nextProps.placeholder});
            };
        };
        componentDidMount() {
            this.setState({ value: this.props.value,
                disabled: this.props.disabled,
                checked: this.props.checked,
                className:this.props.className,
                maxLength:this.props.maxLength,
                placeholder:this.props.placeholder
            });                                        
        };
        handleBlur(event) {
            if ((this.props.checkError===null)||(this.props.checkError(event,false) === true)) {
                this.setState({ value: event.target.value,
                    oldValue: event.target.value
                })
            }
            else
            {
                this.setState({ value: this.state.oldValue })
            }
            this.setState({ backgroundColor: ''})
        };

        handleChange(event) {
            if (this.state.value !== event.target.value) {
                this.setState({ value: event.target.value })
                if ((this.props.checkError!==null)&&(this.props.checkError(event,true) === false)) {
                    this.setState({ backgroundColor: 'red'})
                }
                else
                {
                    this.setState({ backgroundColor: ''})
                }
            }
            if (this.props.onClick!==null) {
                this.props.onClick();
            }
        };
        render() {  
            return <input value={this.state.value} 
                        maxLength={this.state.maxLength} 
                        placeholder={this.state.placeholder} 
                        className={this.state.className}
                        id={this.props.id}
                        name={this.props.name}
                        type={this.props.type}
                        disabled={this.state.disabled} 
                        checked={this.state.checked} 
                        onBlur={this.handleBlur}
                        onChange={this.handleChange}
                        style={{background:this.state.backgroundColor}}/>
    }
    };
    Input.propTypes= 
    {
        value:React.PropTypes.string,
        placeholder:React.PropTypes.string,
        maxLength: React.PropTypes.number,
        disabled:React.PropTypes.bool,
        checked:React.PropTypes.bool,
        className:React.PropTypes.string,
        id:React.PropTypes.string,
        name:React.PropTypes.string,
        type:React.PropTypes.string,
        checkError: React.PropTypes.func,
        onClick: React.PropTypes.func
    }
    Input.defaultProps =
    {
        placeholder:'',
        maxLength:100,
        disabled:false,
        checked:false,
        value:'',
        className:'',
        id:'',
        name:'',
        type:'text',
        checkError:null,
        onClick:null
    }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Saving fragment state after screen rotation

From Dev

stop MediaPlayer after changing the screen

From Dev

React - How to Update State After AJAX Request

From Dev

Changing state doesn't remove input values in React

From Dev

React state changing when pushing nested object

From Dev

React js changing state does not update component

From Dev

Reload page fails after changing state with $state.transitionTo

From Dev

React - Ways to update state after ajax post request

From Dev

Changing the state of a React Component

From Dev

Angular: how to call a function after changing state

From Dev

Ubuntu Screen Flickering after Changing Brightness

From Dev

Buttons dissapearing after changing State UI Routing

From Dev

How to force update to proxy setting after changing registry values in batch script?

From Dev

Update React components sharing state

From Dev

redux update state value without changing initial state

From Dev

React state changing when pushing nested object

From Dev

How to store values in react, that are not state changing?

From Dev

Black screen after changing systemctl

From Dev

update state from child in React

From Dev

Refresh overview scene after changing state in another scene with react / redux / react-native-router-flex

From Dev

Error at Login Screen after changing the background of Login Screen (Ubuntu 19.04)

From Dev

Preserve enter animation after changing screen orientation

From Dev

React handle state update

From Dev

React setState doesnt update state while changing an image

From Dev

screen not re-rendering after changing state

From Dev

React component do not gets updated after changing the the state in componentDidMount

From Dev

changing state before next keydown in react

From Dev

React state empty when changing route

From Dev

Application resets to initial screen after form submitting and changing state

Related Related

  1. 1

    Saving fragment state after screen rotation

  2. 2

    stop MediaPlayer after changing the screen

  3. 3

    React - How to Update State After AJAX Request

  4. 4

    Changing state doesn't remove input values in React

  5. 5

    React state changing when pushing nested object

  6. 6

    React js changing state does not update component

  7. 7

    Reload page fails after changing state with $state.transitionTo

  8. 8

    React - Ways to update state after ajax post request

  9. 9

    Changing the state of a React Component

  10. 10

    Angular: how to call a function after changing state

  11. 11

    Ubuntu Screen Flickering after Changing Brightness

  12. 12

    Buttons dissapearing after changing State UI Routing

  13. 13

    How to force update to proxy setting after changing registry values in batch script?

  14. 14

    Update React components sharing state

  15. 15

    redux update state value without changing initial state

  16. 16

    React state changing when pushing nested object

  17. 17

    How to store values in react, that are not state changing?

  18. 18

    Black screen after changing systemctl

  19. 19

    update state from child in React

  20. 20

    Refresh overview scene after changing state in another scene with react / redux / react-native-router-flex

  21. 21

    Error at Login Screen after changing the background of Login Screen (Ubuntu 19.04)

  22. 22

    Preserve enter animation after changing screen orientation

  23. 23

    React handle state update

  24. 24

    React setState doesnt update state while changing an image

  25. 25

    screen not re-rendering after changing state

  26. 26

    React component do not gets updated after changing the the state in componentDidMount

  27. 27

    changing state before next keydown in react

  28. 28

    React state empty when changing route

  29. 29

    Application resets to initial screen after form submitting and changing state

HotTag

Archive