Create버튼을 클릭하면 mode가 create로 바뀌고 해당하는 UI가 표시되도록..!

<a href="/creat" onClick={event=>{
event.preventDefault();
setMode('CREATE');
}}>Create</a>
setMode('CREATE')const [mode, setMode] = useState('WELCOME');
else if(mode === 'CREATE'){
content = <Create></Create>
}
Create컴포넌트
function Create(props){
return <article>
<h2>Create</h2>
<form>
<p><input type="text" name="title" placeholder='title'/></p>
<p><textarea name="body" placeholder='body'></textarea></p>
<p><input type="submit" value="Create"/></p>
</form>
</article>
}
else if(mode === 'CREATE'){
content = <Create onCreate={(_title, _body)=>{
const newTopic = {id:nextId, title:_title, body:_body}
const newTopics = [...topics]
newTopics.push(newTopic)
setTopics(newTopics);
setMode('READ');
setId(nextId);
setNextId(nextId+1);
}}></Create>
}
onCreate의 호출
function Create(){
return <article>
<h2>Create</h2>
<form onSubmit={event=>{
event.preventDefault();
const title = event.target.title.value;
const body = event.target.body.value;
props.onCreate(title, body);
}}>
<p><input type="text" name="title" placeholder='title'/></p>
<p><textarea name="body" placeholder='body'></textarea></p>
<p><input type="submit" value="Create"/></p>
</form>
</article>
}
onSubmit이라는 prop을 제공하는 것.onSubmit은 submit버튼을 클릭했을 때 form태그에서 발생하는 이벤트const title = event.target.title.value; title의 값const body = event.target.body.value; .body의 값function Create(props){
return <article>
<h2>Create</h2>
<form onSubmit={event=>{
event.preventDefault();
const title = event.target.title.value;
const body = event.target.body.value;
props.onCreate(title, body);
}}>
<p><input type="text" name="title" placeholder='title'/></p>
<p><textarea name="body" placeholder='body'></textarea></p>
<p><input type="submit" value="Create"/></p>
</form>
</article>
}
function App() {
const [mode, setMode] = useState('WELCOME');
const [id, setId] = useState(null);
const [nextId, setNextId] = useState(4);
const [topics, setTopics] = useState([
{id:1, title:'html', body:'html is...'},
{id:2, title:'css', body:'css is...'},
{id:3, title:'javascript', body:'javascript is...'},
]);
let content = null;
if(mode === 'WELCOME'){
content = <Article title="Welcome" body="Hello, WEB"></Article>
}else if(mode === 'READ'){
let title, body = null;
for(let i=0; i<topics.length; i++){
console.log(topics[i].id, id)
if(topics[i].id === id){
title = topics[i].title;
body = topics[i].body;
}
}
content = <Article title={title} body={body}></Article>
}else if(mode === 'CREATE'){
content = <Create onCreate={(_title, _body)=>{
const newTopic = {id:nextId, title:_title, body:_body}
const newTopics = [...topics]
newTopics.push(newTopic)
setTopics(newTopics);
setMode('READ');
setId(nextId);
setNextId(nextId+1);
}}></Create>
}