Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import React, {Component} from 'react';
- class App extends Component {
- constructor(props){
- super(props)
- this.onHandleUsers = this.onHandleUsers.bind(this)
- this.tekstas = "a";
- this.state = {
- isLoaded: false,
- users: []
- }
- }
- handleUserChange = (e) => {
- const {value} = e.target
- this.onHandleCurrentUsers(value)
- this.tekstas = this.onHandleCurrentUsers(value)
- }
- onHandleUsers() {
- fetch("http://localhost:3000/api/users")
- .then(res => res.json())
- .then(result => {
- this.setState({
- isLoaded: true,
- users: result
- })
- })
- .catch(e => console.log(e))
- }
- onHandleCurrentUsers(userId) {
- const data = {userId: userId}
- fetch("http://localhost:3000/api/user", {
- method: 'POST',
- headers: { "Content-Type": "application/json"},
- body: JSON.stringify(data)
- })
- .then(res => res.json())
- .then(result => {
- this.setState({
- isLoaded: false,
- currentUser: result
- })
- })
- .catch(e => console.log(e))
- }
- render() {
- const {currentUser} = this.state
- return (
- <div>
- <div>
- {
- currentUser && <div>{currentUser.name} {currentUser.surname}</div>
- }
- </div>
- <div>
- <form onSubmit={this.handleUserChange}>
- <label>
- <input
- name="userId"
- type="number"
- //value={this.handleUserChange}
- onChange={this.handleUserChange}
- />
- </label>
- <input type="submit" value="Submit" />
- </form>
- {this.tekstas}
- </div>
- <div> </div>
- </div>
- )
- }
- }
- export default App
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement