Praleisti ir pereiti prie pagrindinio turinio

React: handling from events

All.js

import React, { Component } from 'react';
import ReactDOM from 'react-dom';

class App extends Component {
 //konstruojam state
 constructor(props){
  super(props);
  //state body laikysime textarea turini
  this.state ={ 
   body:''
  }
  // tam kad pasiektume per this naudojam modeli bind 
  this.handleSubmit = this.handleSubmit.bind(this);
  this.handleChange = this.handleChange.bind(this);
 }
  

 handleSubmit(e){
  e.preventDefault(); //stop refresh after submit
  console.log(this.state.body);
 }  

 handleChange(e){
  //nustatome stato body lygų textarea turiniui
  this.setState({
   body: e.target.value
  })
 } 

 render() {
  return (
   <div className="container">
    ....
    <form onSubmit={this.handleSubmit}>
     <div className="form-group">
      <textarea 
       onChange={this.handleChange}
       className="form-control" 
       rows="5" 
       maxLength="140" 
       placeholder="whats up?" 
       required/>
     </div>   
     <input type="submit" value="Post" className="form-control" />      </form>       
    ....
   </div>
  );)}

export default App;

Rezultatas:

Komentarai

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

VS Code: Simple react snippets

Simple React Snippets Snippets Snippet Renders imr Import React imrc Import React / Component impt Import PropTypes impc Import React / PureComponent cc Class Component ccc Class Component With Constructor sfc Stateless Function Component cdm componentDidMount cwm componentWillMount cwrp componentWillReceiveProps gds getDerivedStateFromProps scu shouldComponentUpdate cwu componentWillUpdate cdu componentDidUpdate cwu componentWillUpdate cdc componentDidCatch gsbu getSnapshotBeforeUpdate ss setState ssf Functional setState ren render rprop Render Prop hoc Higher Order Component Full Expansions imr - Import React import React from 'react'; imrc - Import React, Component import React, { Component } from 'react'; impt - Import PropTypes import PropTypes from 'prop-types'; impc - Import PureComponent import React, { PureComponent } from 'react'; cc - Class Component class | extends Component { state = { | }, ...

Į dešimtainį

toFixed(2) - verčia skaičių į dešimtainį, skliaustuose nurodyta kiek bus skaičių po kablelio. var bePvm= 10.251 + 13.991 + 57.151; var Pvm= bePvm* 0.21; var viso= bePvm + Pvm; console.log(viso.toFixed(2));