SHARE
TWEET

Untitled

WillmanCodes Oct 21st, 2019 77 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class SomeComponent extends React.Component {
  2.     state={
  3.         caregivers:[ { idCaregiver: '1', caregiverName: 'User1', otherData:'data1'}, { idCaregiver: '2', caregiverName: 'User2', otherData:'data2' },],
  4.         Caregiver_idCaregiver:''
  5.     }
  6.  
  7.     saveUser = (e) => {
  8.         e.preventDefault();
  9.         const user = {Caregiver_idCaregiver : this.state.Caregiver_idCaregiver};
  10.         console.log(user)
  11.         const foundCaregiverObj = this.state.caregivers.find(caregiver=> caregiver.idCaregiver === this.state.Caregiver_idCaregiver)
  12.         console.log(foundCaregiverObj)
  13.     }
  14.  
  15.     onChange = (e) =>{
  16.         this.setState({ [e.target.name]: e.target.value });
  17.     }
  18.  
  19.     formatOptions=(options)=>{
  20.         return options.map(option=>{
  21.             return {value: option.idCaregiver , label:option.caregiverName}
  22.         })
  23.     }
  24.     render(){
  25.         console.log(this.formatOptions(this.state.caregivers))
  26.         return (
  27.             <div>
  28.                    <label>Caregiver:</label>
  29.                     <Select options={ this.formatOptions(this.state.caregivers) } name="Caregiver_idCaregiver"  value={this.state.Caregiver_idCaregiver} onChange={(e)=>this.onChange({target:{...e , name:'Caregiver_idCaregiver'}})}/>
  30.                     <button className="btn btn-success" onClick={this.saveUser}>Save</button>
  31.             </div>
  32.         )
  33.     }
  34. }
  35.  
  36. ReactDOM.render(
  37.     <SomeComponent />,
  38.     document.getElementById("root")
  39. );
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top