mercredi 13 mai 2020

Why forEach if returns []?

I have an array with some orders, the name the array is getOrdersBySalesman. The orders have some attributes : id, client, stage. I want to filter the order by stage, but I it doesn't work, When I select for example "PENDENT" why the console.log is [] I need to send the result of filterOrders to another component and I don't have result. What is wrong????

const Comandes = () => {
  const [orderStage, setOrderStage] = useState('')

  useEffect(() => {
    setOrderStage(orderStage)
  }, [orderStage])

  let filterOrders = []

  const filterStage = newStage => {
    getOrdersBySalesman.forEach(order => {
       if (order.stage === newStage) {
        filterOrders.push(order)
      }
    });
    setOrderStage(newStage)
  }
  console.log(filterOrders) = > IS []

  return (
    <div>
      <Layout>
        <select
          value={orderStage}
          onChange={e => filterStage(e.target.value)}
        >
          <option value="TOTS ELS ESTATS">TOTS ELS ESTATS</option>
          <option value="ACABADA">ACABADA</option>
          <option value="PENDENT">PENDENT</option>
          <option value="CANCELADA">CANCELADA</option>
        </select>
      {filterOrders.length === 0 ? (
          <p className="mt-5 text-center text-2xl">Encara no hi ha comandes </p>
        ) : (filterOrders.map(order => (
          <Order key={order.id} order={order} />
        ))
          )}
      </Layout>
    </div >
  )
}
export default Comandes

Aucun commentaire:

Enregistrer un commentaire