You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Introduce the concept of server actions to enable direct function execution on the server from the client side without needing to create traditional API routes. Developers can define server-side logic in their React components and call these server actions from the client.
Example
// ./src/pages/home.jsximport{useAction}from'metassr/hooks';// Import the useAction hook from MetaSSR's node module// Home page componentexportdefaultfunctionHome(){// Hook to fetch results from server actionsconst{ all,GET,POST,PUT,DELETE}=useAction();// Example usage of action resultsconsole.log('All actions:',all);console.log('GET action result:',GET);console.log('POST action result:',POST);console.log('PUT action result:',PUT);console.log('DELETE action result:',DELETE);return(<div><h1>Hello World</h1><p>Data from server actions will be logged in the console.</p></div>);}// Default server action to handle general operationsexportfunctionaction(){// Perform operations on the server for any request// Example logging message (will appear in server logs)console.log('Executing default action on server');constresult={message: 'Default action result'};returnresult;}// Action for handling GET requestsexportfunctionaction$GET(){// Perform operations specific to GET requests// Example logging message (will appear in server logs)console.log('Executing GET action on server');constresult={message: 'GET request result'};returnresult;}// Action for handling POST requestsexportfunctionaction$POST(){// Perform operations specific to POST requests// Example logging message (will appear in server logs)console.log('Executing POST action on server');constresult={message: 'POST request result'};returnresult;}// Action for handling PUT requestsexportfunctionaction$PUT(){// Perform operations specific to PUT requests// Example logging message (will appear in server logs)console.log('Executing PUT action on server');constresult={message: 'PUT request result'};returnresult;}// Action for handling DELETE requestsexportfunctionaction$DELETE(){// Perform operations specific to DELETE requests// Example logging message (will appear in server logs)console.log('Executing DELETE action on server');constresult={message: 'DELETE request result'};returnresult;}
Benefits
Simplifies communication between the client and server by allowing server-side functions to be invoked directly from the client.
Reduces boilerplate for API creation, making it easier to manage server-side logic within components.
Improves data fetching and mutation patterns, leading to more seamless integration of server-side and client-side operations.
The text was updated successfully, but these errors were encountered:
Description
Introduce the concept of server actions to enable direct function execution on the server from the client side without needing to create traditional API routes. Developers can define server-side logic in their React components and call these server actions from the client.
Example
Benefits
The text was updated successfully, but these errors were encountered: