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.