Unverified Commit 09c8eae4 authored by SaiGanesh21's avatar SaiGanesh21 Committed by GitHub

docs: Example mismatch when changing language JS/TS (#61833)

### What? 
Resolves issue #61695 

### Why?
The `Building Your Application -> Data Fetching -> Server Actions and
Mutations` section in the Next.js documentation has a few mismatches
with respect to JS and TS codes.

---------
Co-authored-by: 's avatarbattuAshita <battua@iitbhilai.ac.in>
Co-authored-by: 's avatarSam Ko <sam@vercel.com>
parent 1da90c27
......@@ -584,22 +584,38 @@ While it's common to use Server Actions within `<form>` elements, they can also
You can invoke a Server Action from event handlers such as `onClick`. For example, to increment a like count:
```js filename="app/actions.js" switcher
'use server'
```tsx filename="app/like-button.tsx" switcher
'use client'
import { incrementLike } from './actions'
import { useState } from 'react'
export default function LikeButton({ initialLikes }: { initialLikes: number }) {
const [likes, setLikes] = useState(initialLikes)
export async function incrementLike() {
// Mutate database
// Return updated data
return (
<>
<p>Total Likes: {likes}</p>
<button
onClick={async () => {
const updatedLikes = await incrementLike()
setLikes(updatedLikes)
}}
>
Like
</button>
</>
)
}
```
```tsx filename="app/like-button.tsx" switcher
```jsx filename="app/like-button.js" switcher
'use client'
import { incrementLike } from './actions'
import { useState } from 'react'
export default function LikeButton({ initialLikes }: { initialLikes: number }) {
export default function LikeButton({ initialLikes }) {
const [likes, setLikes] = useState(initialLikes)
return (
......@@ -670,6 +686,28 @@ export default function ViewCount({ initialViews }: { initialViews: number }) {
}
```
```jsx filename="app/view-count.js" switcher
'use client'
import { incrementViews } from './actions'
import { useState, useEffect } from 'react'
export default function ViewCount({ initialViews }: { initialViews: number }) {
const [views, setViews] = useState(initialViews)
useEffect(() => {
const updateViews = async () => {
const updatedViews = await incrementViews()
setViews(updatedViews)
}
updateViews()
}, [])
return <p>Total Views: {views}</p>
}
```
Remember to consider the [behavior and caveats](https://react.dev/reference/react/useEffect#caveats) of `useEffect`.
### Error Handling
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment