Thursday 18 May 2017

How to add/delete objects in array?

The challenge is to add or delete objects in/from ingredients. I have to work with JSON. It looks like this:

{
  "_id": "57ed9c6022e125000f2a7146",
  "title": "Kuře na ssfd",
  "preparationTime": 75,
  "servingCount": 4,
  "sideDish": "těstoviny",
  "directions": "",
  "user": "Kubík",
  "__v": 0,
  "slug": null,
  "ingredients": [
    {
      "_id": "57ed9c6022e125000f2a714b",
      "amountUnit": "g",
      "amount": 500,
      "name": "kuřecí maso"
    },
    {
      "_id": "57ed9c6022e125000f2a714a",
      "amountUnit": "ks",
      "amount": 1,
      "name": "cibule"
    },

My code:

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

this.state = {
  recipe: {},
  isFetching: true,
  isSaving: false,

}
}

componentWillMount() {
api()
  .get(`/recipes/${this.props.params.slug}`)
  .then((response) => {
    this.setState({
      recipe: response.data,
      isFetching: false,
    });
  });
 }
render() {

const { isFetching, recipe} = this.state;

if (isFetching) {
  return <Loader />;
}

return ( );
}
}



via Ubiday

No comments:

Post a Comment