import { useState } from 'react';
import './App.css';
import axios from 'axios';
import { Todo } from './Todo';
type TodoType = {
userId: number;
title: string;
completed: boolean;
};
export default function App() {
const [todos, setTodos] = useState<Array<TodoType>>([]);
const onClickFetchData = () => {
axios
.get<Array<TodoType>>('https://jsonplaceholder.typicode.com/todos')
.then((res) => {
setTodos(res.data);
});
};
return (
<div className="App">
<button onClick={onClickFetchData}>データ取得</button>
{todos.map((todo) => (
<Todo
title={todo.title}
userId={todo.userId}
completed={todo.completed}
/>
))}
</div>
);
}
type TodoType = { userId: number; title: string; completed?: boolean };
export const Todo = (props: TodoType) => {
const { title, userId, completed = false } = props;
const completedMark = completed ? '{完}' : '{未}';
return <p>{`${completedMark}${title}(ユーザ:${userId})`}</p>;
};
export type TodoType = {
userId: number;
id: number;
title: string;
completed: boolean;
};
import { useState } from 'react';
import './App.css';
import axios from 'axios';
import { Todo } from './Todo';
import { TodoType } from './types/todo';
export default function App() {
const [todos, setTodos] = useState<Array<TodoType>>([]);
const onClickFetchData = () => {
axios
.get<Array<TodoType>>('https://jsonplaceholder.typicode.com/todos')
.then((res) => {
setTodos(res.data);
});
};
return (
<div className="App">
<button onClick={onClickFetchData}>データ取得</button>
{todos.map((todo) => (
<Todo
key={todo.id}
title={todo.title}
userId={todo.userId}
completed={todo.completed}
/>
))}
</div>
);
}
import { TodoType } from './types/todo';
export const Todo = (props: Omit<TodoType, 'id'>) => {
const { title, userId, completed = false } = props;
const completedMark = completed ? '{完}' : '{未}';
return <p>{`${completedMark}${title}(ユーザ:${userId})`}</p>;
};
import { FC } from 'react';
type Props = {
color: string;
fontSize: string;
};
export const Text: FC<Props> = (props) => {
const { color, fontSize } = props;
return <p style=>テキストです</p>;
};
export type User = {
name: string;
hobbies?: Array<string>;
};
import { User } from './types/user';
import { FC } from 'react';
type Props = {
user: User;
};
export const UserProfile: FC<Props> = (props) => {
const { user } = props;
return (
<dl>
<dt>名前</dt>
<dd>{user.name}</dd>
<dt>趣味</dt>
<dd>{user.hobbies?.join('/')}</dd>
</dl>
);
};