0

I have made a shopping cart in react. I have assigned a value to each button. The value shows in the dom so I know it's there.

I created a function to capture the value and then push it into an array. However, when I hit add to cart it say's "nothing added". I've been trying to find a way to listen for the value in the event listener but nothing seems to work.

I have tried putting just e.target.value but the outcome is the same.

Any ideas?

const [item, setItem] = useState([]);

function addCart(e) {
    if (e.target.value === "") {
      item.push(e.target.value);
      console.log(item);
      setCart(cart + 1);
    } else {
      console.log("nothing added");
    }
  }

 <button
              value={pro.price}
              onClick={addCart}
              className="bg-blue-500 text-white font-bold border-white p-2 rounded-md"
            >
              {"add to cart"}
            </button>
2
  • 1
    You have a state function called setItem that you're not using. Commented Jan 27, 2022 at 14:44
  • if (e.target.value === "") { - So... only add the value to the array if the value is empty? If pro.price has any value other than an empty string then this would always invoke the else block. Was that just a typo? Commented Jan 27, 2022 at 14:45

3 Answers 3

1

I think you need to negate the condition in the if block. e.target.value !== ""

Sign up to request clarification or add additional context in comments.

Comments

0

If you want to update your state item, you've to use setItem.

function addCart(e) {
    if (e.target.value !== "") {
      setItem([...item, e.target.value]);
      console.log(item);
      setCart(cart + 1);
    } else {
      console.log("nothing added");
    }
  }

2 Comments

sorry, why do i need setItem? im still learning coming from just vanilla js. I added the '!==' and that performed what i needed.
When you say it "works", do you mean your cart state is correctly incremented ? If so, yes it works. But your item state has been mutated using push which returns the lenght of the new array and not an array. In React, you've to use setters functions to update states.
0

I suggest that you could change your approach and use a class instead of a function as such :

class AddCard extends React.Component {
  constructor(props) {
    super(props)
    this.state = {
      value: 'VALUE'
    }
  }
  handleChange (e) {
    console.log('handleChange called')
  }
  handleClick () {
    this.setState({value: 'UPDATED_VALUE'})
    var event = new Event('input', { bubbles: true });
    this.myinput.dispatchEvent(event);
  }
  render () {
    return (
      <div>
        <input readOnly value={this.state.value} onChange={(e) => {this.handleChange(e)}} ref={(input)=> this.myinput = input}/>
        <button onClick={this.handleClick.bind(this)}>Change Input</button>
      </div>
    )
  }
}

ReactDOM.render(<AddCard />,  document.getElementById('app'))

By doing so you'll be able to dissociate the click event and the change event

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.