mercredi 6 mai 2020

ReactJS Toggling between states of array [hooks]

Hy everyone, Can you help me please with the problem I have?

So, I have a checkbox and an array of objs:

const req = [
{status: "pending", type: "aaa"},
{status: "pending", type: "bbb"},
{status: "pending", type: "bbb"},
{status: "pending", type: "aaa"},
{status: "pending", type: "aaa"}
]


const [ownRequest, setOwnRequest] = useState([]);

const handleCheck = (event) => {
    setChecked(!checked);

    let newArray = ownRequest.map((a) => {
      let newObject = {};
      Object.keys(a).forEach((propertyKey) => {
        newObject[propertyKey] = a[propertyKey];
      });
      return newObject;
    });

    let result = checked
      ? newArray
      : newArray.filter(({ type}) => type === "aaa");
    setOwnRequest(result);
}

<Checkbox onChange={(e) => handleCheck(e)} checked={checked} />

I would like to display the items like this: if is checked show me all items, if not only the items with type:"aaa", and also to have the posibility to come back to original state (like a toggle)

I did something, but I can't figure it out how to handle it further




Aucun commentaire:

Enregistrer un commentaire