4
\$\begingroup\$

I'm trying to rewrite my old project in Reactjs. As I am a beginner and this is my first app, I need some assistance in understanding what is the best way to do this in the React way and how I can improve myself and this demo project.

Here is a working demo of it.

ListManager.js

import React, { Component } from 'react'
import List from './List'

export class ListManager extends Component {
    constructor(props, context) {
       super(props, context)
       this.state = { finaldata :[]}
}


APIres(ids){
    Promise.all(ids.map(id =>
        fetch('http://api.tvmaze.com/shows/'+id).then(resp => resp.json().then(data => data.name))
        )).then(data => {
            data.sort()
            this.setState({finaldata:data})
        })
}

componentWillMount(){
    const id = [1,2,3,4,5]
    this.APIres(id)

}

render() {

    return ( 
        < div >
            <h3>{this.props.title}< /h3> 

            <List data={this.state.finaldata} />
        < /div >
    )
  }
}

List.js

'use strict'
import React, { Component } from 'react'

const ListItem = (props) => <li>{ props.text }</li>

const List = (props) => {
    const listNodes = props.data.map((
    value,index) => {
        return(
            <ListItem text={value} key={index}></ListItem>
        )
    })

    return ( <ul>{listNodes}</ul>
    )
}

export default List

Some note: id array will come with a URL query like example.com/#/?id=1,2,3,4. I will be using react-router for this.

\$\endgroup\$
6
  • \$\begingroup\$ @200_success thanks for edit . last line was important . i want to show name released to id as soon its request its done . i know for that Promise is wrong thing to use for it . \$\endgroup\$ Commented Sep 6, 2016 at 3:54
  • \$\begingroup\$ Code Review is a site for open-ended critique of code that already works as intended. If you happen to get an answer that suggests a way to load results in parallel, then great. But asking how to implement it would be off-topic. \$\endgroup\$ Commented Sep 6, 2016 at 3:58
  • \$\begingroup\$ Please don't take this as an insult -- i don't know how to phrase this in a softer way -- if this code is formatted the same way as your files are, you may want to read a few javascript style guidelines, make some decisions based on what you like, and install some tool such as eslint to help you keep your style consistent. Personally, I like the airbnb style guide: github.com/airbnb/javascript \$\endgroup\$ Commented Dec 21, 2016 at 21:15
  • \$\begingroup\$ I use this one: github.com/feross/standard. It's excellent and its look nice. I don't know why my code looks like this. I tried to fix in StackOverflow, but I was failed. Look at this code: prntscr.com/dmrw7l \$\endgroup\$ Commented Dec 22, 2016 at 20:40
  • \$\begingroup\$ need help in this now ?? \$\endgroup\$ Commented Jan 16, 2017 at 9:36

1 Answer 1

1
\$\begingroup\$

A few things to change. Move the api request to componentDidMount because componentWillMount is considered legacy now. Rename the method thats making the api request to be more declarative which is to be more descriptive of whats its doing. Move the list of ids into the method thats making the api request. All of those changes + the changes to the api request from your question here https://stackoverflow.com/questions/39473302/show-result-from-parallel-ajax-request-in-react-component/39486654#39486654 would look like this:

import React, { Component } from "react";
import List from "./List";

class ListManager extends Component {
  constructor(props) {
    super(props);

    this.state = {
      listData: []
    };

    this.getListData = this.getListData.bind(this);
  }

  componentDidMount() {
    this.getListData();
  }

  getListData() {
    const ids = [1, 2, 3, 4, 5];

    let listData = [];

    ids.forEach(id =>
      fetch("http://api.tvmaze.com/shows/" + id)
        .then(resp => resp.json())
        .then(show => {
          listData.push(show);
        })
    );

    this.setState({
      listData: listData.sort((a, b) => a.runtime - b.runtime)
    });
  }

  render() {
    return (
      <div>
        <h3>{this.props.title}</h3>
        <List data={this.state.listData} />
      </div>
    );
  }
}

export default ListManager;
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.