Experiment 7 (FSD)
Experiment 7 (FSD)
AIM:
PROCEDURE:
61781922106041
constructor(props) {
super(props);
this.state = {
todos: [],
errorMessage: '',
inputId: ''
};
this.handleSubmit = this.handleSubmit.bind(this);
this.handleChange = this.handleChange.bind(this);
handleChange(event) {
handleSubmit(event) {
event.preventDefault();
return;
axios.get(`https://jsonplaceholder.typicode.com/todos?id=${inputId}`)
.then(response => {
61781922106041
if (response.data.length > 0) {
} else {
this.setState({ todos: [], errorMessage: "No TODO found for this ID" });
})
.catch(error => {
console.error(error);
});
render() {
return (
<div>
<form onSubmit={this.handleSubmit}>
<h1>Enter ID</h1>
<input
type="text"
name="inputId"
value={inputId}
onChange={this.handleChange}
/>
<br />
<button type="submit">Submit</button>
</form>
61781922106041
<div>
<h1>Todo List</h1>
todos.map(todo => (
<div key={todo.id}>
<h2>Id: {todo.id}</h2>
<h2>Title: {todo.title}</h2>
<hr />
</div>
))
errorMessage &&
{errorMessage}
</div>
</div>
</div>
);
OUTPUT:
61781922106041
constructor(props) {
super(props)
this.state = {
userId: '',
title: '',
body: '',
output: {}
this.handleSubmit=this.handleSubmit.bind(this)
this.handleChange=this.handleChange.bind(this)
handleChange(event) {
this.setState({[event.target.name]:event.target.value})
handleSubmit(event) {
event.preventDefault();
console.log(this.state);
axios.post('https://jsonplaceholder.typicode.com/posts',this.state)
.then(response=>
console.log("winner",response.data);
this.setState({output:response.data})
})
.catch(error=>console.log(error))
61781922106041
render() {
const {userId,title,body,output}=this.state
return (
<div>
<form onSubmit={this.handleSubmit}>
<br/>
<br/>
<h1> Body</h1>
</div>
OUTPUT:
61781922106041
if (!userId.trim()) {
return;
try {
`https://jsonplaceholder.typicode.com/todos?userId=${userId}`
);
setCompletedTodos(completed);
setUncompletedTodos(uncompleted);
setError("");
61781922106041
} catch (err) {
console.error(err);
};
return (
<input
type="text"
value={userId}
placeholder="Enter userId"
/>
Fetch Todos
</button>
<div>
<thead>
<tr>
61781922106041
<th>ID</th>
<th>Title</th>
<th>Completed</th>
</tr>
</thead>
<tbody>
{completedTodos.map((todo) => (
<tr key={todo.id}>
<td>{todo.id}</td>
<td>{todo.title}</td>
<td>{todo.completed.toString()}</td>
</tr>
))}
</tbody>
</table>
</div>
)}
<thead>
<tr>
<th>ID</th>
<th>Title</th>
<th>Completed</th>
61781922106041
</tr>
</thead>
<tbody>
{uncompletedTodos.map((todo) => (
<tr key={todo.id}>
<td>{todo.id}</td>
<td>{todo.title}</td>
<td>{todo.completed.toString()}</td>
</tr>
))}
</tbody>
</table>
</div>
)}
</div>
);
};
61781922106041
RESULT: