REACT HOOKS USESTATE ve FORM ELEMANLARI

Musa BAL
1 min readJul 25, 2021

--

App.js

import React from “react”

import TodoList from “./components/TodoList”;

function App() {

return (

<div className=”App”>

<TodoList/>

</div>

);

}

export default App;

TodoList.js

import React, {useState} from “react” // useState import ettik

import AddNewTodo from “./AddNewTodo”

const TodoList =() => { //functional component oluşturuyoruz

const [todos, setTodos] = useState([ //useState kullanarak dizi oluşturduk

{id:1, task:”Faturaları öde”},

{id:2, task:”Arabayı yıka”}

])

const addTodo =(text)=> { // todos dizisine eleman eklemek için fonksiyon yazdık

// text parametresi ile AddNewTodo componentinden veriyi aldık

setTodos([

…todos,

{id:Math.random(), task:text}] // text parametresini kullandık

)

}

return(

<div>

<lu>

{

todos.map (todo=>{ // todos dizisini maple

return(

<li key={todo.id}>{todo.task}</li>

)

})

}

</lu>

{/*<button onClick={addTodo} >Add a todo</button> button onClick olayında fonksiyonu çağır*/}

<AddNewTodo addTodo={addTodo}/> {/* Props olarak ekledik*/}

</div>

)

}

export default TodoList;

AddNewTodo.js

import React,{useState} from “react”

const AddNewTodo =({addTodo})=> { // addTodo parametresi

const [todo, setTodo]=useState(“”)

const onFormSubmit = e =>{

e.preventDefault() // submit ettiğimde formu yeniden yükleme

addTodo(todo) // addTodo fonksiyonuna todo verisini gönder (gelen boş propsu doldurup gönder)

setTodo(“”) // text kutusunun içini boşalt

}

return(

<form onSubmit={onFormSubmit}>

<label htmlFor=”todo”/>

<input type=”text” id=”todo” value={todo} /* text boşaltmak için*/ onChange={(e) => setTodo(e.target.value)} /> {/*text kutusuna her giriş yapıldığında içindeki veriyi setTodo ya gönder*/}

<input type=”submit”/>

</form>

)

}

export default AddNewTodo;

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Musa BAL
Musa BAL

No responses yet

Write a response