Skip to content

Instantly share code, notes, and snippets.

@omas-public
Created December 23, 2021 11:33
Show Gist options
  • Save omas-public/c5c09976f0b7f23d5d5b96d3d43e5f03 to your computer and use it in GitHub Desktop.
Save omas-public/c5c09976f0b7f23d5d5b96d3d43e5f03 to your computer and use it in GitHub Desktop.
import React, { useState, useEffect } from 'react'
const ViewForm = ({ text }) => <p>text: {text}</p>
const ActionForm = ({ fn }) => {
const [inputValue, setInputValue] = useState('')
const handleChange = (e) => {
setInputValue(e.target.value)
}
const handleClick = e => fn(inputValue)
const textReset = () => {
fn('')
setInputValue('')
}
return (
<>
<input value={inputValue} onChange={handleChange} type='text' />
<button onClick={handleClick}>set text</button>
<button onClick={textReset}>reset</button>
</>
)
}
const SampleComponent = () => {
// const [inputValue, setInputValue] = useState('')
const [text, setText] = useState('')
useEffect(() => {
console.log('レンダリング!')
})
const handleClick = (value) => {
setText(value)
// setText(inputValue)
}
// const handleChange = (e) => {
// setInputValue(e.target.value)
// }
// const textReset = () => {
// setText('')
// setInputValue('')
// }
return (
<>
<ActionForm fn={handleClick} />
{/* <input value={inputValue} onChange={handleChange} type='text' />
<button onClick={handleClick}>set text</button>
<button onClick={textReset}>reset</button> */}
<ViewForm text={text} />
{/* <p>text: {text}</p> */}
</>
)
}
export default function App () {
return <SampleComponent />
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment