Advertisement
Denvanrain

right

Nov 3rd, 2020 (edited)
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // state обновляется корректно и функция map() отрабатывает верно!
  2. class Recipes extends Component {
  3.   state = {
  4.     recipes: [],
  5.   };
  6.  
  7.   componentDidMount() {
  8.     this.getData().then(res => {
  9.       this.setState({recipes: res})
  10.     });
  11.   }
  12.  
  13.   getData() {
  14.     const url = '/api/v1/recipes/index';
  15.     const recipes = fetch(url)
  16.       .then(response => response.json())
  17.       .catch(error => console.log(error));
  18.     return recipes;
  19.   }
  20.  
  21.   render() {
  22.     const { recipes } = this.state;
  23.    
  24.     // !!! map() работает
  25.    
  26.     const allRecipes = recipes.map((recipe, index) => (
  27.       <ul key={index}>
  28.         <li>{recipe.id}</li>  
  29.       </ul>
  30.     );
  31.     return (
  32.       <div>{allRecipes}</div>
  33.     )
  34.   }
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement