3

I'm working on a simple list maker, to do list app using create-react-app and I'm having some trouble puzzling out the functionality. What I'm trying to accomplish with this app:

  • I want to be able to enter text into an input, push the button or press enter, and whatever text will be listed on the body of the app.

  • I want to be able to create a button that will delete the list items once the task or objective is complete

My code is broken up into these components so far:

App, ListInput, ItemList, Item

The code for App is

import React, { Component } from 'react';
import './App.css';
import Navigation from './components/Navigation';
import ListInput from './components/ListInput';
import ListName from './components/ListName';
import Item from './components/Item';
import ItemList from './components/ItemList';

class App extends Component {
  constructor() {
    super();
      this.state = {
        input: '',
        items: []
      };    
  }

  addItem = () => {
    this.setState(state => {
      let inputValue = this.input.current.value;
      if (inputValue !== '') {
          this.setState({
            items: [this.state.items, inputValue]
          })
      }
    })
  }


  onButtonEnter = () => {
    this.addItem();
  }

  render() {
    return (
      <div className="App">
       <Navigation />
       <ListName />
       <ListInput addItem={this.addItem}
        onButtonEnter={this.onButtonEnter} />
        <Item />
        <ItemList />
      </div>
    );
  }
}

export default App;

The code for ListInput is :

import React from 'react';
import './ListInput.css';

const ListInput = ({ addItem, onButtonEnter }) =>  {

        return (
            <div>
                <p className='center f2'>
                {'Enter List Item'}
                </p>
                <div className='center'>
                    <div className='center f3 br-6 shadow-5 pa3 '>

                            <input type='text'
                            className='f4 pa2 w-70 center' 
                            placeholder='Enter Here'

                             />                       
                            <button className='w-30 grow f4 link ph3 pv2 dib white bg-black'
                            onClick={onButtonEnter}
                            onSubmit={addItem} >
                                {'Enter'}
                            </button>

                    </div>
                </div>
            </div>
            );

}


export default ListInput;

The code for Item is:

import React from 'react';

const Item = ({text}) =>{
    return (
        <div>
            <ul>{text}</ul>
        </div>
    )}
export default Item;

And the code for ItemList is :

import React from 'react';
import Item from './Item';

const ItemList = ({ items }) => {
    return (
        <div>
            {item.map(items => <Item key={item.id}
            text={item.text} />
            )}
        </div>
    )
}

export default ItemList;

In my react app I am returning an error of 'item' is not defined and I'm confused why.

halfer
  • 19,824
  • 17
  • 99
  • 186
Ashley E.
  • 473
  • 2
  • 6
  • 12

3 Answers3

1

Your ItemList was not correct. Take a look at corrected snippet below you need to map on items and not item (hence the error item is not defined). Also, you need to items as a prop to ItemList in your app.js

import React from 'react';
import Item from './Item';

const ItemList = ({ items }) => {
    return (
        <div>
            {items.map(item => <Item key={item.id}
            text={item.text} />
            )}
        </div>
    )
}

export default ItemList;

In app.js add following line. Also, I don't see what is doing in your app.js remove it.

<ItemList items={this.state.items}/>
Shubham Gupta
  • 2,596
  • 1
  • 8
  • 19
1

In your App.js you need to pass items as a prop to ItemList component like

 <ItemList items={this.state.items} />

Also in addItem function pushing inputValue to items array isn’t correct do something like below

  addItem = () => {
     this.setState(state => {
        let inputValue = this.input.current.value;
        if (inputValue !== '') {
            this.setState(prevState => ({
                items: [...prevState.items, inputValue]
            }));
         }
      })
  }

And in ItemList.js do conditional check before doing .map also some typo errors in .map

 import React from 'react';
 import Item from './Item';

  const ItemList = ({ items }) => {
      return (
          <div>
              {items && items.map(item => <Item key={item.id}
               text={item.text} />
              )}
            </div>
       )
   }

  export default ItemList;

Try with above changes This would work

Please excuse me if there are any typo errors because I am answering from my mobile

Hemadri Dasari
  • 32,666
  • 37
  • 119
  • 162
1

Seems like you have a typo in ItemList.
It receives items (plural) as prop but you are using item.

const ItemList = ({ items }) => {
    return (
        <div>
            {items.map(items => <Item key={item.id}
            text={item.text} />
            )}
        </div>
    )
}

And don't forget to actually pass the items prop to 'ItemList':

<ItemList items={this.state.items} />
Sagiv b.g
  • 30,379
  • 9
  • 68
  • 99