1.1.3 β€’ Published 2 years ago

use-todo v1.1.3

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

πŸ“’ A React Hook for 'Todo List'.

You just wanted to try new cool animation features or ui library and decided to build a simple todo app.

But, even for a simple todo application, you have to create mock states and functions to mutate the states such as addTodo, deleteTodo and so forth.

Now 'use-todo' will give you everything!

npm.io

Language Support

❗️Note: By default, todo item contents are in English. If you want to change contents to korean, provide { lang:"kr" } to options.

❗️Note: νˆ¬λ‘ μ•„μ΄ν…œμ˜ λ‚΄μš©μ€ 기본적으둜 μ˜μ–΄λ‘œ μ„€μ •λ˜μ–΄ μžˆμŠ΅λ‹ˆλ‹€. ν•œκΈ€ 지원을 μ›ν•˜μ‹œλ©΄ options κ°’μœΌλ‘œ { lang:"kr" }을 μ „λ‹¬ν•΄μ£Όμ„Έμš”.

πŸ‘‰ See Options

Installation

Install with npm

npm i use-todo

Install with yarn

yarn add  use-todo

English documentation

  1. How do you display todo list?
  2. How do you add new todo item to the list?
  3. How do you remove a todo item from the list?
  4. How do you edit a todo item from the list?
  5. How do you change completed state for a todo item?
  6. Options?

Korean documentation

  1. κΈ°λ³Έ μ‚¬μš©λ²•
  2. μ•„μ΄ν…œμ€ μ–΄λ–»κ²Œ μΆ”κ°€ν•˜λ‚˜μš”?
  3. μ•„μ΄ν…œμ€ μ–΄λ–»κ²Œ μ‚­μ œν•˜λ‚˜μš”?
  4. μ•„μ΄ν…œμ€ μ–΄λ–»κ²Œ μˆ˜μ •ν•˜λ‚˜μš”?
  5. μ™„λ£Œ μƒνƒœλŠ” μ–΄λ–»κ²Œ λ³€κ²½ν•˜λ‚˜μš”?
  6. Options?

Basic Usage

1️. How do you display todo list?

import React from 'react';

import { useTodo } from 'use-todo';

function TodoComponent() {
    const { todoItems, addTodo, deleteTodo, toggleCompletion } = useTodo();

    return (
        <div>
            {todoItems.map((todo) => {
                return (
                    // Todo Item
                    <div key={todo.id}>
                        <span>{todo.title}</span>
                        <span>{todo.content}</span>
                        <span>{todo.date}</span>
                    </div>
                );
            })}
        </div>
    );
}

export default TodoComponent;

2. How do you add new todo item to the list?

import React from 'react';

import { useTodo } from './lib';

function TodoComponent() {
    const { todoItems, addTodo, deleteTodo, toggleCompletion } = useTodo();

    const [title, setTitle] = React.useState('');
    const [content, setContent] = React.useState('');

    const onButtonClick = () => {
        const newTodoItem = { title, content };
        addTodo(newTodoItem); // πŸ‘ˆ  add new todo item to current todo items state
    };
    return (
        <div>
            {/*πŸ‘‡ Here when button is clicked, 
            you can call addTodo function with title and content value*/}

            <button onClick={onButtonClick}>AddTodo</button>
            <input placeholder="Enter title" value={title} onChange={(e) => setTitle(e.target.value)} />
            <input placeholder="Enter content" value={content} onChange={(e) => setContent(e.target.value)} />
        </div>
    );
}

export default TodoComponent;

3. How do you remove a todo item from the list?

import React from 'react';

import { useTodo } from './lib';

function TodoComponent() {
    const { todoItems, addTodo, deleteTodo, toggleCompletion } = useTodo();

    const onRemoveClicked = (id) => {
        // To remove a todo item from the todo list,
        deleteTodo(id); // πŸ‘ˆ  pass a todo id to deleteTodo function
    };
    return (
        <div>
            {todoItems.map((todo) => {
                return (
                    // Todo Item
                    <div key={todo.id}>
                        <span>{todo.title}</span>
                        <span>{todo.content}</span>
                        <span>{todo.date}</span>
                        <button onClick={() => onRemoveClicked(todo.id)}>Remove Todo</button>
                    </div>
                );
            })}
        </div>
    );
}

export default TodoComponent;

4. How do you edit a todo item from the list?

// React
import React from 'react';
import { useTodo } from './lib';

function App() {
    const { todoItems, editTodo } = useTodo();

    const handleEdit = (id: string) => {
        // πŸ‘‡ Here you pass new todo data and id to editTodo function
        editTodo(id, { title: 'NEW TITLE', content: 'NEW CONTENT' });
    };

    return (
        <>
            {todoItems.map((todo) => {
                if (!todo.completed) {
                    return (
                        <div>
                            <span>{todo.title}</span>
                            <span>{todo.content}</span>
                            <button onClick={() => handleEdit(todo.id)}>Edit Todo</button>
                        </div>
                    );
                }
            })}
        </>
    );
}

5. How do you change completed state for a todo item?

import React from 'react';

import { useTodo } from 'use-todo';

function TodoComponent() {
    const { todoItems, addTodo, deleteTodo, toggleCompletion } = useTodo();

    const handleComplete = (id: string) => {
        // change completed state of a todo item
        toggleCompletion(id); // πŸ‘ˆ  pass a todo id to toggleCompletion function
    };
    return (
        <div>
            {todoItems.map((todo) => {
                return (
                    // Todo Item
                    <div key={todo.id}>
                        <span>{todo.title}</span>
                        <span>{todo.content}</span>
                        <span>{todo.date}</span>
                        <button onClick={() => handleComplete(todo.id)}>Complete</button>
                    </div>
                );
            })}
        </div>
    );
}

export default TodoComponent;

Options

const options = {
    dataNum: 10, // πŸ‘ˆ  Determines initial number of todo items in todo list
    contentLength: 20, // πŸ‘ˆ  Determines the length of todo content
    useLocalStorage: true, // πŸ‘ˆ  Stores todo list state to browser local storage
    lang: 'kr' // πŸ‘ˆ  change default language for todo contents to korean
};
const { todoItems, addTodo, deleteTodo, toggleCompletion } = useTodo(options);
1.1.3

2 years ago

1.1.2

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.1.0

2 years ago

1.1.1

2 years ago

0.0.8

2 years ago

0.0.7

2 years ago

0.0.6

2 years ago

0.0.5

2 years ago

0.0.4

2 years ago

0.0.3

2 years ago

0.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago