1

I want to add a new fruit to the current list of fruit but I am unsure how to go about doing it, I am currently posting an empty string on submit, which just adds an empty string li to the list

my fruit list component is as follow;

import AddFruit from './AddFruit';

class Fruits extends Component {
  state = {
    fruits: ['apple', 'banana']
  };

  render() {
    const { fruits } = this.state;
    return (
      <div style={{ paddingTop: 30 }}>
        <AddFruit handleSubmit={this.handleSubmit} />
        {fruits.map(fruit => {
          return (
            <ul key={fruit}>
              <li>{fruit}</li>
            </ul>
          );
        })}
      </div>
    );
  }

  handleSubmit = e => {
    const { fruits } = this.state;
    e.preventDefault();
    this.setState({ fruits });
  };
}

export default Fruits;

and my fruit adder is as follows... I know they are totally wrong but I'm having a tough time figuring this out and its late :(


class AddFruit extends Component {
  state = {
    addItem: ''
  };
  render() {
    const { addItem } = this.state;
    return (
      <div>
        <form onSubmit={this.handleSubmit} style={{ paddingTop: 35 }}>
          <input
            type="text"
            value={addItem}
            placeholder="Add Item"
            onChange={this.handleChange}
          />
          <button
            onClick={this.props.handleSubmit}
            className="btn btn-primary btn-sm"
          >
            Add Fruit
          </button>
        </form>
      </div>
    );
  }

  handleChange = event => {
    console.log(event.target.value);
    const { value } = event.target;
    this.setState({ addItem: value });
  };
}

export default AddFruit;

2 Answers 2

1

it is not clear what kind of element invokes HandleSubmit, but lets say it is input so the code is

handleSubmit = e => {
  e.preventDefault();
  const fruitName = e.target.value;
  const fruits = [...this.state.fruits, fruitName];
  this.setState({ fruits });
};
Sign up to request clarification or add additional context in comments.

1 Comment

great.. thanks for your help. I have amended the question a little as I had posted the same code snippet twice. You can now see how the handleSubmit is being handled. Unfortunately, I am posting an empty string only with your reply :/
0

This is a problem on how to pass data from child component to its parent in ReactJS:

Fruits

  <AddFruit onFruitsChange={this.onFruitsChange} />

  onFruitsChange = fruit => {
    const { fruits } = this.state;
    fruits = [ ...fruits, fruit]
    this.setState({ fruits });
  };

AddFruit

<button
        onClick={this.onAddFruit}
        className="btn btn-primary btn-sm"
      >

  onAddFruit = (e) => {
    e.preventDefault();
    const fruit = this.state.addItem;
    this.props.onFruitsChange(fruit)
  };

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.