import './App.css';
// import Item from './myItem'
import React from 'react';
class StarWars extends React.Component{
constructor(){
super()
this.state={
loadedCharacter:false,
name:null,
height:null,
homeworld:null,
eye_color:null,
}
}
newCharacterFromBtn(){
const randomNum=Math.round(Math.random()*82)
const url=`https://swapi-node.vercel.app/api/people/${randomNum}/`
fetch(url)
.then(response => response.json())
.then(data =>{
console.log(data)
this.setState(
{ loadedCharacter:true,
name:data.name,
height:data.height,
homeworld:data.homeworld,
eye_color:data.eye_color,
})
})
}
render(){
return(
{
this.state.loadedCharacter &&
name:{this.state.name}
height:{this.state.height}
homeWorld:{this.state.homeworld}
- eyecolor:{this.state.eye_color}
this.newCharacterFromBtn()} type="button" className="btn">Randomize Character
)
}
}
function App() {
return (
);
}
export default App;
```
Подробнее здесь: https://stackoverflow.com/questions/796 ... -your-page
Мобильная версия