
Reactをセットアップするための環境設定
はじめに React(リアクト)は、Facebook(現Meta)が開発したJavaScriptライブラリで、主にWebアプリケーションの**ユーザーインターフ […]
<div class="container">
<h1 id="title">Hello World</h1>
<p>Welcome to React</p>
</div>
<div>
<p>Sample Component</p>
</div>
<div>
<img src="image.jpg" alt="Sample">
<button disabled={false}>Click me</button>
</div>
function Welcome() {
return <h1>Welcome!</h1>;
}
function MultiElements() {
return (
<h1>Title</h1>
<p>Description</p>
);
}
function Greeting() {
return <div>Hello!</div>;
}
// 選択肢A
class Counter extends React.Component {
state = { count: 0 };
}
// 選択肢B
class Counter extends React.Component {
constructor() {
state = { count: 0 };
}
}
// 選択肢C
class Counter extends React.Component {
constructor(props) {
super(props);
this.state = { count: 0 };
}
}
function ItemList(props) {
return <div>Items will be listed here</div>;
}
function Greeting({ isLoggedIn }) { // ここにコードを記述 }
const numbers = [1, 2, 3, 4, 5];
jsx const user = { name: 'John Doe', age: 30, email: 'john@example.com' };
function UserProfile() {
return (
<div className="profile">
<div className="header">
<h1>User Profile</h1>
</div>
<div className="content">
<p>Name: John Doe</p>
<p>Email: john@example.com</p>
</div>
</div>
);
}
function Welcome(props) {
return <h1>Hello, {props.name}</h1>;
}
jsx function User({ name, age, isVerified }) { return ( ¨K64K ); }
class Counter extends React.Component { constructor(props) { super(props); this.state = { count: 0 }; } // ここにメソッドを追加 }
this.setState({ counter: this.state.counter + 1 }); this.setState({ counter: this.state.counter + 1 });
state = { user: { name: 'John', age: 30 } }; // 更新方法A this.setState({ user: { age: 31 } }); // 更新方法B this.setState({ user: { ...this.state.user, age: 31 } });
state = { items: ['apple', 'banana'] }; // 方法A this.state.items.push('orange'); this.setState({ items: this.state.items }); // 方法B this.setState({ items: [...this.state.items, 'orange'] });
function DataDisplay({ isLoading, error, data }) { // ここにコードを記述 }
function ItemList({ items }) {
return (
<ul>
{items.map(item => (
<li key={item.id}>{item.name}</li>
))}
</ul>
);
}
class Counter extends React.Component {
state = { count: 0 };
increment = () => {
this.setState(prevState => ({ count: prevState.count + 1 }));
};
componentDidUpdate(prevProps, prevState) {
// ここにコードを記述
}
render() {
return (
<div>
<p>Count: {this.state.count}</p>
<button onClick={this.increment}>Increment</button>
</div>
);
}
}
<div className="container">
<h1 id="title">Hello World</h1>
<p>Welcome to React</p>
</div>
<div>
{/* これはサンプルコンポーネントです */}
<p>Sample Component</p>
</div>
<div>
<img src="image.jpg" alt="Sample" />
<button disabled={false}>Click me</button>
</div>
function HelloReact() {
return <h1>Hello, React!</h1>;
}
const Welcome = () => <h1>Welcome!</h1>;
function MultiElements() {
return (
<>
<h1>Title</h1>
<p>Description</p>
</>
);
}
function Greeting({ name }) { return <div>Hello, {name}!</div>; }
function ItemList({ items }) {
return (
<ul>
{items.map((item, index) => (
<li key={index}>{item}</li>
))}
</ul>
);
}
function Greeting({ isLoggedIn }) {
return isLoggedIn ? <p>Welcome back!</p> : <p>Please sign in</p>;
}
function NumberList() {
const numbers = [1, 2, 3, 4, 5];
return (
<ul>
{numbers.map((number) => (
<li key={number.toString()}>{number}</li>
))}
</ul>
);
}
function UserInfo() {
const user = { name: 'John Doe', age: 30, email: 'john@example.com' };
return (
<div>
<p>Name: {user.name}</p>
<p>Age: {user.age}</p>
<p>Email: {user.email}</p>
</div>
);
}
// 親コンポーネント
function Parent() {
return <Child title="Welcome" content="Hello World" />;
}
// 子コンポーネント
function Child({ title, content }) {
return (
<div>
<h1>{title}</h1>
<p>{content}</p>
</div>
);
}
function Header() {
return (
<div className="header">
<h1>User Profile</h1>
</div>
);
}
function Content() {
return (
<div className="content">
<p>Name: John Doe</p>
<p>Email: john@example.com</p>
</div>
);
}
function UserProfile() {
return (
<div className="profile">
<Header />
<Content />
</div>
);
}
function Welcome(props) {
return <h1>Hello, {props.name}</h1>;
}
Welcome.defaultProps = {
name: 'Guest'
};
function Parent() {
return (
<Child>
<p>This will be rendered as children</p>
</Child>
);
}
function Child({ children }) {
return <div>{children}</div>;
}
import PropTypes from 'prop-types';
function User({ name, age, isVerified }) {
return (
<div>
<h2>{name}</h2>
<p>Age: {age}</p>
{isVerified && <span>Verified</span>}
</div>
);
}
User.propTypes = {
name: PropTypes.string.isRequired,
age: PropTypes.number,
isVerified: PropTypes.bool
};
User.defaultProps = {
isVerified: false
};
increment = () => { this.setState(prevState => ({ count: prevState.count + 1 })); };
function Counter() {
const [count, setCount] = useState(0);
return (
<div>
<p>Count: {count}</p>
<button onClick={() => setCount(count + 1)}>Increment</button>
</div>
);
}
this.setState(prevState => ({ counter: prevState.counter + 1 }));
this.setState(prevState => ({ counter: prevState.counter + 1 }));
function DataDisplay({ isLoading, error, data }) {
if (isLoading) return <Spinner />;
if (error) return <div>Error: {error.message}</div>;
return <div>{data}</div>;
}
function ItemList({ items }) {
return items.length === 0 ? (
<p>No items found</p>
) : (
<ul>
{items.map(item => (
<li key={item.id}>{item.name}</li>
))}
</ul>
);
}
function Card({ title, children }) {
return (
<div className="card">
<div className="card-header">
<h2>{title}</h2>
</div>
<div className="card-body">{children}</div>
</div>
);
}
function withLoading(Component) {
return function EnhancedComponent({ isLoading, ...props }) {
if (isLoading) return <div>Loading...</div>;
return <Component {...props} />;
};
}
componentDidUpdate(prevProps, prevState) {
if (prevState.count % 5 !== 0 && this.state.count % 5 === 0) {
console.log(`Count is now ${this.state.count}, which is a multiple of 5`);
}
}
class BatchExample extends React.Component {
state = { count: 0 };
increment = () => {
this.setState({ count: this.state.count + 1 });
this.setState({ count: this.state.count + 1 });
this.setState({ count: this.state.count + 1 });
// 最終的にcountは1だけ増える(バッチ処理される)
};
render() {
return (
<div>
<p>Count: {this.state.count}</p>
<button onClick={this.increment}>Increment</button>
</div>
);
}
}
// 親コンポーネント
function Parent() {
const handleChildClick = (message) => {
console.log(message);
};
return <Child onClick={handleChildClick} />;
}
// 子コンポーネント
function Child({ onClick }) {
return <button onClick={() => onClick('Child was clicked')>Click me</button>;
}
function Alert() {
return <div className="alert">Alert!</div>;
}
function Warning() {
return <div className="warning">Warning!</div>;
}
function DynamicComponent({ type }) {
const components = { alert: Alert, warning: Warning };
const Component = components[type] || null;
return Component ? <Component /> : null;
}