Created
December 23, 2021 11:33
-
-
Save omas-public/c5c09976f0b7f23d5d5b96d3d43e5f03 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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