JavaScript React setState only returning last item in a list saved to variable react setstate only if changed,react setstate only one property,

I am very new to React, currently doing a small project of using GitHub API to return a search result via AJAX and be able to list that result on the screen. Currently I am using a for loop to iterate over the response data and saving that data to a variable, there is most likely a way better way to do this but like I said I am new. I then set the state to the returned data. The issue is in the setState it is only returning the last result saved to the variable and not the entire variable. Listed below is the entire component, any tips or advice would be greatly appreciated. Thank you!

import axios from 'axios';
import * as React from 'react';


class User extends React.Component<any, any> {


    constructor(props: any) {
        super(props);
        this.state = {
            name: name,
            id: '',
            userInput: '',
            obj: null
        };
    }

    handleSubmit(e: any) {
        axios.get('https://api.github.com/users/' + this.state.userInput + '/repos')
            .then((response) => {
                if (response.data.length > 0) {
                    console.log('success');
                    let data1 = JSON.stringify(response.data);
                    let result = JSON.parse(data1);
                    for (let key in result) {
                        let obj = result[key];
                        let test = obj.name;
                        console.log(test);
                        this.setState({
                             name: name,
                             id: result[0].id,
                             obj: test,
                             userInput: this.state.userInput
                        })
                    };

                } else {
                    console.log('else is working');
                }
            })
            .catch((error) => {
                console.log('error ');
            });
    }

    render() {
        return (
            <div>
                <form>
                    <input type="text" onChange={this.handleUserInput.bind(this)} value={this.state.userInput} />
                    <h1>{this.state.userInput}</h1>
                    <button type="button" onClick={this.handleSubmit.bind(this)}>Submit</button>
                </form>
                <h1>Name : {this.state.name}</h1>
                <h1> ID : {this.state.id}</h1>
                <h1>OBJ : {this.state.obj}</h1>
            </div>
        );
    }
}

export default User;

The result from consoling the variable test gives this console output console output

However when it is being set in obj by this.state.obj it is only showing the last item as shown here written to page

Answer:1

Every time you call setState, you overwrite the previous state. React tries to intelligently merge the provided (new) state and the previous state, but if there are any key collisions, the previous state (of that key) will be lost. If you want a list of items in your state, you'll have to do something like

let items = [];
for (...) {
    items.push({
        name: name,
        id: result[0].id,
        ...
    });
}

this.setState({ items: items }); 

following, you can access each item in the list by using this.state.items[someIndex] etc

Answer:2

I'm working on a simple web form for teachers in my building to generate HTML pages with embedded videos to use locally (our substitutes can't get on the Internet to play videos; this is a workaround)....

I'm working on a simple web form for teachers in my building to generate HTML pages with embedded videos to use locally (our substitutes can't get on the Internet to play videos; this is a workaround)....

  1. iframe source from html
  2. source from folder html
  3. javascript source from html
  4. download source from html
  5. source html
  6. source html tag
  7. source html code
  8. source html element
  9. source html image
  10. source html online
  11. html5 source
  12. source html video
  13. source html viewer
  14. source html css
  15. source html chrome
  16. source html media
  17. source html picture
  18. source html img
  19. source html editor
  20. source html page

I am trying to authenticate a user from Angular 2 app by calling an express api that provides a JWT token upon success. I have got a doubt to clear. Do we ask express to set the cookie or is it ...

I am trying to authenticate a user from Angular 2 app by calling an express api that provides a JWT token upon success. I have got a doubt to clear. Do we ask express to set the cookie or is it ...

  1. xsrf-token cookie angular

I'm trying to create a component using vue.js, where the component contains a font-awesome icon, which is supplied in template. For example, I'd like to be able to use <big-nav-item icon="fa-phone"...

I'm trying to create a component using vue.js, where the component contains a font-awesome icon, which is supplied in template. For example, I'd like to be able to use <big-nav-item icon="fa-phone"...

I'm in the middle of implementing server sent event using Django. HTML <script type='text/javascript'> var source = new EventSource('/pro_listen'); console.log(source); source....

I'm in the middle of implementing server sent event using Django. HTML <script type='text/javascript'> var source = new EventSource('/pro_listen'); console.log(source); source....

  1. setup server side sync dynamics 365
  2. setup server side rules outlook 2013
  3. setup server side rules outlook 2010
  4. setup server side synchronization
  5. setup server side
  6. setup react server side rendering
  7. outlook setup server side rules
  8. setup guide server-side synchronization for crm online
  9. sql server setup server side trace
  10. how to setup server side exchange rules
  11. dynamics crm setup server side sync