Created
October 2, 2019 14:44
-
-
Save cziem/28908e2c795aaa26f0157fa73bb5e42c to your computer and use it in GitHub Desktop.
Add post using the useReducer
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, { useReducer } from "react"; | |
const AddPost = () => { | |
const [postState, setPostState] = useReducer( | |
(state, newState) => ({ ...state, ...newState }), | |
{ | |
title: "", | |
body: "" | |
} | |
); | |
const handleChange = e => { | |
setPostState({ [e.target.name]: e.target.value }); | |
}; | |
return ( | |
<div> | |
<form> | |
<input | |
type="text" | |
value={postState.title} | |
name="title" | |
onChange={handleChange} | |
/> | |
<br /> | |
<input | |
type="text" | |
value={postState.body} | |
name="body" | |
onChange={handleChange} | |
/> | |
<br /> | |
<button>Publish</button> | |
</form> | |
</div> | |
); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment