Skip to content

Instantly share code, notes, and snippets.

@omas-public
Last active November 22, 2021 10:42
Show Gist options
  • Select an option

  • Save omas-public/cf1086bd2ccf8b02a08918f3a2ea3e95 to your computer and use it in GitHub Desktop.

Select an option

Save omas-public/cf1086bd2ccf8b02a08918f3a2ea3e95 to your computer and use it in GitHub Desktop.
// 関数コンポーネント内で state を扱えるようにするため、React を import 時に useState を読み込む
import React, { useState } from 'react'
import './App.css'
const VueForm = ({selectedValue}) => (
<p>
現在選択されている値:<b>{selectedValue}</b>
</p>
)
const SelectItems = ({selectedValue, handleChange, values}) => (
<select value={selectedValue} onChange={handleChange}>
{
values.map(v =>
<option value={v.item} key={v.id}>{v.item}</option>)
}
</select>
)
const InputSelectBox = ({values}) => {
const [selectedValue, setSelectedValue] = useState('HTML')
const handleChange = (e) => {
setSelectedValue(e.target.value)
}
return (
<div className='App'>
<VueForm selectedValue={selectedValue} />
<SelectItems selectedValue = {selectedValue} handleChange={handleChange} values={values}/>
</div>
)
}
export default function App () {
const values = [
{id: 1, item: "HTML"},
{id: 2, item: "CSS"},
{id: 3, item: "JavaScript"}
]
return <InputSelectBox values = {values}/>
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment