Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to add the component which allows adding an article to a list

I need to add the ListArticle component which allows adding an article to a list.

import React, { useState } from "react";
export default function ListArticle() {
  const [id, setId] = useState(0);
  const [designation, setDesignation] = useState("");
  const [prix, setPrix] = useState(0);
  const [Article, setArticle] = useState([]);
  const handlerOnChangeId = (e) => {
    setId(e.target.value);
  };
  const handlerOnChangeDesignation = (e) => {
    setDesignation(e.target.value);
  };
  const handlerOnChangePrix = (e) => {
    setPrix(e.target.value);
  };
  const handlerAddArticle = (e) => {
    setArticle(e.target.Article);
  };
  return (
    <div className="container">
      <div className="list">
        <h2 className="title">Ajout d'un Article</h2>
        <div>
          <label>Id:</label>
          <input type="text" onChange={handlerOnChangeId} value={id} />
        </div>
        <div>
          <label>designation:</label>
          <input type="text" onChange={handlerOnChangeDesignation} value={designation}/>{" "}
        </div>{" "}
        <div>
          {" "}
          <label>prix:</label>
          <input type="text" onChange={handlerOnChangePrix} value={prix} />{" "}
        </div>{" "}
        <div>
          {" "}
          <input type="button" value="Ajouter" onClick={handlerAddArticle}/>{" "}
        </div>{" "}
        <div>
          {" "}
          <h3>liste Articles</h3>
          <ul>{Article}</ul>{" "}
        </div>{" "}
      </div>{" "}
    </div>
  );
}

when I click on the button the function "handlerAddArticle" shows the article like that

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

The problem is that the list doesn’t show when the function is being used

const handlerAddArticle = (e) => {
    setArticle(e.target.Article);
  };

>Solution :

Need to add the item to the list

setArticle(prev => [...prev, {id,designation,prix}]);

use map to show items in the dom

 <ul>{Article.map(i => <><li>{i.id} | {i.designation}  | {i.prix} </li></>)}</ul>{" "}

Demo

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading