Let's go down a more client component driven approach, to submit the new post form. This will allow us to take control by running logic on the client, before sending the data to the server.
step 1:
Update the create post page to be a client component. This will remove the server action for now but we'll add that back in later.
This "client" version of the create post page has more room for a better user experience. Right now there is an arbitraty requirement that a post must be at least 3 characters long, but hopefully you see the potential for more complex validation and a better user experience.
Let's break down what's going on in this big code block.
useState
import{ useState }from"react"//1
export defaultfunctionCreatePost(){
const [content,setContent] =useState("")//2
return(
<form>
<textarea
name="content"
required
value={content}//3
onChange={(e)=>setContent(e.target.value)}//4
/>
<divclassName="text-neutral-500">
Characters:{content.length} {/*5*/}
</div>
</form>
)
}
import{ useState }from"react"// 1
export default functionCreatePost(){
const [content,setContent] =useState("")// 2
return(
<form>
<textarea
name="content"
required
value={content}// 3
onChange={(e)=>setContent(e.target.value)}// 4
/>
<divclassName="text-neutral-500">
Characters:{content.length} {/* 5 */}
</div>
</form>
)
}
import{ useState }from"react"//1
export defaultfunctionCreatePost(){
const [content,setContent] =useState("")//2
return(
<form>
<textarea
name="content"
required
value={content}//3
onChange={(e)=>setContent(e.target.value)}//4
/>
<divclassName="text-neutral-500">
Characters:{content.length} {/*5*/}
</div>
</form>
)
}
import{ useState }from"react"// 1
export default functionCreatePost(){
const [content,setContent] =useState("")// 2
return(
<form>
<textarea
name="content"
required
value={content}// 3
onChange={(e)=>setContent(e.target.value)}// 4
/>
<divclassName="text-neutral-500">
Characters:{content.length} {/* 5 */}
</div>
</form>
)
}
Import useState
Create a state for the text input
Set the value of the text area to be the value of the state variable
When the value changes, update the state
Present the value of the state variable as the character count
buttonDisabled
export defaultfunctionCreatePost(){
const [content,setContent] =useState("")
constbuttonDisabled=content.length<=3
return(
<form>
<button
type="submit"
className={buttonDisabled&&"opacity-50"}
disabled={buttonDisabled}
aria-disabled={buttonDisabled}
>
Post
</button>
</form>
)
}
export default functionCreatePost(){
const [content,setContent] =useState("")
constbuttonDisabled=content.length<=3
return(
<form>
<button
type="submit"
className={buttonDisabled&&"opacity-50"}
disabled={buttonDisabled}
aria-disabled={buttonDisabled}
>
Post
</button>
</form>
)
}
export defaultfunctionCreatePost(){
const [content,setContent] =useState("")
constbuttonDisabled=content.length<=3
return(
<form>
<button
type="submit"
className={buttonDisabled&&"opacity-50"}
disabled={buttonDisabled}
aria-disabled={buttonDisabled}
>
Post
</button>
</form>
)
}
export default functionCreatePost(){
const [content,setContent] =useState("")
constbuttonDisabled=content.length<=3
return(
<form>
<button
type="submit"
className={buttonDisabled&&"opacity-50"}
disabled={buttonDisabled}
aria-disabled={buttonDisabled}
>
Post
</button>
</form>
)
}
Create a buttonDisabled variable. This is plain variable, not a state variable, but it's based on the value of the state variable content.
Only disable the button when that variable is true
handleSubmit
export defaultfunctionCreatePost(){
consthandleSubmit=(e:React.FormEvent)=> {
e.preventDefault()
console.log(content)
}
return(
<formonSubmit={handleSubmit}>
</form>
)
}
export default functionCreatePost(){
consthandleSubmit=(e:React.FormEvent)=> {
e.preventDefault()
console.log(content)
}
return(
<formonSubmit={handleSubmit}>
</form>
)
}
export defaultfunctionCreatePost(){
consthandleSubmit=(e:React.FormEvent)=> {
e.preventDefault()
console.log(content)
}
return(
<formonSubmit={handleSubmit}>
</form>
)
}
export default functionCreatePost(){
consthandleSubmit=(e:React.FormEvent)=> {
e.preventDefault()
console.log(content)
}
return(
<formonSubmit={handleSubmit}>
</form>
)
}
Create a handleSubmit function that just logs the content to the console.
Add the handleSubmit function to the form's onSubmit prop
If you test out this component, obviously no network requests happen and no data is stored in the database. When we submit the form, we get a console log in the browser and that's it. But by adding client side JavaScript, we can create a more unique experience for the user.
But this is all worthless without actually being able to save the data to the database. Let's create a server action!
But there's one issue, we can't create a server action in a client component.
This makes sense if you think about it. All code in a client component will run on the client (web browser) so obviously we can't run server-side code there.
step 1:
Create a new file inside of create-post called actions.ts.
src
app
page.tsx
create-post
page.tsx
actions.ts(server actions)
As the name implies, this file will contain our server action.
Adding "use server" ensures that all code within this file will only ever run on the server.
This function is going to be invoked using client side JavaScript, so we will send the content as an argument instead of FormData.
We can do some basic validation here. This is going to be a duplicate of the client side validation.
Now we can try to insert the data into the database and handle any errors that might occur.
If everything goes well, we can revalidate the home page and redirect the user back to the home page.
step 3:
Update the handleSubmit function on the create post page to use this server action.
create-post/page.tsx
"use client"
import{ createPost }from'./actions'
export defaultfunctionCreatePost(){
consthandleSubmit=async(e:React.FormEvent)=> {
e.preventDefault()
constresult=awaitcreatePost(content)
if(result.error){
console.log(error)
//handle the error
return
}
setContent("")
}
//...
}
create-post/page.tsx
"use client"
import{ createPost }from'./actions'
export default functionCreatePost(){
consthandleSubmit= async(e:React.FormEvent)=> {
e.preventDefault()
constresult= awaitcreatePost(content)
if(result.error){
console.log(error)
// handle the error
return
}
setContent("")
}
// ...
}
create-post/page.tsx
"use client"
import{ createPost }from'./actions'
export defaultfunctionCreatePost(){
consthandleSubmit=async(e:React.FormEvent)=> {
e.preventDefault()
constresult=awaitcreatePost(content)
if(result.error){
console.log(error)
//handle the error
return
}
setContent("")
}
//...
}
create-post/page.tsx
"use client"
import{ createPost }from'./actions'
export default functionCreatePost(){
consthandleSubmit= async(e:React.FormEvent)=> {
e.preventDefault()
constresult= awaitcreatePost(content)
if(result.error){
console.log(error)
// handle the error
return
}
setContent("")
}
// ...
}
import{ createPost }from'./actions'//1
consthandleSubmit=async(e:React.FormEvent)=> {
e.preventDefault()
constresult=awaitcreatePost(content)//2
//3
if(result.error){
console.log(error)
//handle the error
return
}
setContent("")//4
}
import{ createPost }from'./actions'// 1
consthandleSubmit= async(e:React.FormEvent)=> {
e.preventDefault()
constresult= awaitcreatePost(content)// 2
// 3
if(result.error){
console.log(error)
// handle the error
return
}
setContent("")// 4
}
import{ createPost }from'./actions'//1
consthandleSubmit=async(e:React.FormEvent)=> {
e.preventDefault()
constresult=awaitcreatePost(content)//2
//3
if(result.error){
console.log(error)
//handle the error
return
}
setContent("")//4
}
import{ createPost }from'./actions'// 1
consthandleSubmit= async(e:React.FormEvent)=> {
e.preventDefault()
constresult= awaitcreatePost(content)// 2
// 3
if(result.error){
console.log(error)
// handle the error
return
}
setContent("")// 4
}
Import the action into the client component. The code won't run on the client because of the "use server" directive at the top of the file.
Call the function directly from the client side code. This will make an HTTP request and post the data.
Handle any server side errors. We'll need more state for that.
Reset the form after the post is created.
This is similar to the form submission we had before, but now we have a lot more control over the process. One drawback is that this won't work without client side JavaScript enabled, however, this process is really common and makes for a better user experience.