Praleisti ir pereiti prie pagrindinio turinio

Laravel: React kaupimas masyve

//resources/assets/js/components/App.js

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


class App extends Component {

    constructor(props){
        super(props);
        //saugosime žinutes posts masyve
        this.state ={ 
            body:'',
            posts: []
        }
        // bind 
        this.handleSubmit = this.handleSubmit.bind(this);
        this.handleChange = this.handleChange.bind(this);
    }
  

    handleSubmit(e){
       e.preventDefault(); //stop refresh after submit
       // siunčiame post su turiniu body
       // gautą atsakymą išvedame ir sujungiame su buvusiu post masyvu
       //išvalome body
        axios.post('./posts',{
            body: this.state.body
        }).then(response => {
            console.log(response);
            this.setState({
                posts: [...this.state.posts, response.data],
                body: ''
            })
        });
    }    

    postData(){
        axios.post('./posts',{
            body: this.state.body
        });
    }

    handleChange(e){
       this.setState({
            body: e.target.value
       })
    } 

    render() {
        return (
            <div className="container">
                <div className="row justify-content-center">
                    <div className="col-md-6">
                        <div className="card">
                            <div className="card-header">Tweet something</div>

                            <div className="card-body">
                                <form onSubmit={this.handleSubmit}>
                                    <div className="form-group">
                                        <textarea 
                                            onChange={this.handleChange}
                                            value={this.state.body}
                                            className="form-control" 
                                            rows="5" 
                                            maxLength="140" 
                                            placeholder="whats up?" 
                                            required/>
                                    </div>   
                                        <input
                                            type="submit"
                                            value="Post"
                                            className="form-control" 
                                            />   

                                </form>       
                            </div>
                        </div>
                    </div>
                    <div className="col-md-6">
                        <div className="card">
                            <div className="card-header">Recent tweets</div>

                            <div className="card-body">
state esačius įrašus mapinam į atskirus elementus ir juos atvaizduojame
key reikalingas react'ui atskirti elementus
{this.state.posts.map(post => <div key={post.id}>{post.body}</div>)}
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        );
    }
}

export default App;

rezultate:




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));