Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

235
Visualizações
How to pass select-options value to button onclick in react?

I can pass the value of my select-options but I change my mind and want to pass the value first on the button for onClick function.. Thanks

import { useSelector, useDispatch } from "react-redux";
const Purchase = () => {
  const products = useSelector(state => state.products);
  const dispatch = useDispatch();
  const purchaseHandler = e => {
      let pName = e.target.options[e.target.selectedIndex].text;
      let price = e.target.value;
      let obj = { pName, price };
      dispatch({ type: "PURCHASE", payLoad: obj });
  };
  return (
      <div>
          <select onChange={e => purchaseHandler(e)}>
              {products.map((product, index) => {
                  return (
                      <option value={product.price} key={index}>
                          {product.pName} - ${product.price}
                      </option>
                  );
              })}
          </select>
          <button onClick={purchaseHandler} className="addToCart">
              Add to Cart
          </button>
      </div>
  );
};
export default Purchase;

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Save productIndex when selection changes and get the product from products array using index when clicking the button.

Try like below

import { useSelector, useDispatch } from "react-redux";

const Purchase = () => {
    const products = useSelector(state => state.products);
    const dispatch = useDispatch();

    const [productIndex, setProductIndex] = useState(0);

    const purchaseHandler = () => {
        dispatch({ type: "PURCHASE", payLoad: products[productIndex] });
    };
    return (
        <div>
            <select
                onChange={e => {
                    setProductIndex(e.target.value);
                }}
            >
                {products.map((product, index) => {
                    return (
                        <option value={index} key={index}>
                            {product.pName} - ${product.price}
                        </option>
                    );
                })}
            </select>
            <button onClick={purchaseHandler} className="addToCart">
                Add to Cart
            </button>
        </div>
    );
};

export default Purchase;

Edit wonderful-banzai-dbwgk

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda