Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a simple list maker app in React.JS?

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.

like image 979
Ashley E. Avatar asked Mar 22 '26 11:03

Ashley E.


1 Answers

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

like image 71
Hemadri Dasari Avatar answered Mar 25 '26 01:03

Hemadri Dasari



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!