# Server Actions ## Actions - Called explicitly - Data is returned ```typescript export const action = serverAction$((params: {name: string}) => { // add to DB return 42; }); export const Cmp1 = component$(() => { return <button onClick$={() => { // runs in the server const number = await action(); // 42 }}> Click </button> }); export const CmpForm = component$(() => { return ( <Form action={action}> <input name="name"> </Form> ) }); ``` ## Loaders - Called implicitally - Data is collected ```typescript export const loader = serverLoader$(ctx) => { if (isSignin(ctx)) { return { name: 'manu' }; } else { return { name: 'anonymous' }; } }); export const action = serverAction$() => { // add to DB setCookie(login); return true; }); function action() { const state = useLoaderState(); const BODY = await fetch(POST); { action: 42, loaders: { 'fds': 23445, '53454' } }; loc.redo++; } export const CmpForm = component$(() => { const resource = loader.get(); return ( <Resource value={resource} onResolve={(data) => { return () <button onClick$={() => { const worked = await action({name:'asdf'}); }} > {data.name} </button> ) }}/> ) });
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up