Advertisement
cunha1

Untitled

Feb 2nd, 2020
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React from 'react';
  2.  
  3. class misc {
  4.     static makeid(length) {
  5.         var result           = '';
  6.         var characters       = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
  7.         var charactersLength = characters.length;
  8.         for ( var i = 0; i < length; i++ ) {
  9.         result += characters.charAt(Math.floor(Math.random() * charactersLength));
  10.         }
  11.         return result;
  12.     }
  13. }
  14.  
  15. class Multitest extends React.Component {
  16.     state = {
  17.         elements: {
  18.             bicboi: 2,
  19.             sbd: "gucci",
  20.             baksaj: "std"
  21.         }
  22.     }
  23.    
  24.     constructor(props) {
  25.         super(props);
  26.         this.addToObject = this.addToObject.bind(this);
  27.     }
  28.     render () {
  29.         let renderthis = [];
  30.         Object.keys(this.state.elements).forEach((key,i) => {
  31.             renderthis.push(<div key={i}>{key}: {this.state.elements[key]}</div>);
  32.         });
  33.         return (
  34.             <>
  35.               <button onClick={this.addToObject}>Add to object</button>
  36.               {renderthis}
  37.             </>
  38.         )
  39.     }
  40.     addToObject() {
  41.         let newElements = this.state.elements;
  42.         const key = misc.makeid(8);
  43.         Object.assign(newElements, {[key]: Math.random()});
  44.         this.setState({elements:newElements});
  45.     }
  46.  
  47. }
  48.  
  49. export default Multitest;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement