Praleisti ir pereiti prie pagrindinio turinio

React: event, destructor, inline style, key, join

Kodo pavyzdys [naudojant axios] gautų duomenų talpinimas į State. Pridėtas evant'as, reguojantis į paspaudimą. Naudojimo inline style pavyzdys. Masyvų sujungimas, siekiant neprarasti jau turimų state duomenų. Bindinimas. Pasikartojantiems elementams key suteikimas - nes visi React elementai turi turėti savo unikalius numerius, todėl pasikartojančius reikia papildomai juos 'išskirti'.   

//App.js
import React, { Component } from 'react';
import axios from 'axios'
import Loading from './Loading'



class App extends Component {
  constructor(props){
    super(props)
    //state
    this.state ={
      users: [],
      loading: false
    }
    //bind - tinkamo sąryšio su kviečiant onclicku
    this.handleSubmit = this.handleSubmit.bind(this)
  }

  getUsers(){
    this.setState({loading:true})
    axios('https://api.randomuser.me/?nat=US&results=5').then(response => this.setState({
      //turimo state sujungimas su gautais duomenimis
      users: [...this.state.users, ...response.data.results],
      loading: false
    }))
  }

  //metodas paspaudimo reakcijai
  handleSubmit(e){
    e.preventDefault();
    this.getUsers();
    console.log('more users loaded');
  }

  componentWillMount(){
    this.getUsers()  
  }

  render() {
    //destruktorius iš stato
    const {loading, users} = this.state;
    return (
      <div>
      <form onSubmit={this.handleSubmit}>
            <input type="submit" value="load more..."/>
          </form>
        {!loading ? users.map(user => (
          <div key={user.id.value}>
          <h3 style={{color:'red'}}>{user.name.first} {user.name.last}</h3>
          <p>{user.email}</p>
          <hr />
          
          </div>
        )) : <Loading message="Loading!!"/>}
      </div>
    );
  }
}

export default App;

Komentarai

Populiarūs šio tinklaraščio įrašai

React: Material Design for Bootstrap 4

https://mdbootstrap.com 1.  create-react-app yourappnamehere 2.  cd yourappnamehere 3. Add  mdbreact  as a dependency to the package.json (the  #react-upgrade  one) 4.  yarn  /  npm  shall install all the dependencies. Note, that  mdbreact  itself includes Bootstrap ( 4.0.0-beta.2 ) as its dependency. Once all of it is out of the way, 5. time to modify your  App.js  file. To utilize full power of  MBD React  there, you need to import components you would like to use, for example import {Button, NavbarBrand, Navbar, NavbarNav, NavItem} from 'mdbreact'; and secondly, you need proper CSS files. As MDB partially builds upon Bootstrap, it's safest to include both: import '../node_modules/bootstrap/dist/css/bootstrap.min.css'; import '../node_modules/mdbreact/docs/css/mdb.min.css'; As you can see, there is no jQuery involved. The warning popping out during  npm install  /  yarn stems ...

String sudėjimas

String 'sudėjimas' - sujungimas dviejų (arba bent vieno ir kito number) string duomenų. Pvz: "Labas"+"Lietuva" "Labas" + " Lietuva" "Dabar" + 20+17 "Dabar" + " 20"+"17"