React 與 Qwik 的比較
幾個比較 React 和 Qwik 的範例。
Hello world 元件
⚛️ 從 React 開始
export const HelloWorld = () => {
return <div>Hello world</div>;
}
⚡️ 轉換至 Qwik
export const HelloWorld = component$(() => {
return <div>Hello world</div>;
});
具有點擊處理程式的按鈕
⚛️ 從 React 轉換
export const Button = () => {
return <button onClick={() => console.log('click')}>Click me</button>;
}
⚡️ 轉換至 Qwik
export const Button = component$(() => {
return <button onClick$={() => console.log('click')}>Click me</button>;
});
還記得 $ 嗎?
Qwik 中的事件處理程式行為與其他框架中的行為相同,您只需記住,由於使用了貨幣符號後綴,因此它們的內容將以延遲方式載入。
注意 ⚠️:JSX 處理程式(例如 onClick$ 和 onInput$)僅在客戶端執行。這是因為它們是 DOM 事件,由於伺服器上沒有 DOM,因此它們不會在伺服器上執行。
宣告本地狀態
⚛️ 從 React 轉換
export function UseStateExample() {
const [value, setValue] = useState(0);
return <div>Value is: {value}</div>;
}
⚡️ 轉換至 Qwik
export const LocalStateExample = component$(() => {
const count = useSignal(0);
return <div>Value is: {count.value}</div>;
});
如果我有一個更複雜的狀態怎麼辦?
useStore() 的工作原理與 useSignal() 非常相似,但它採用一個物件作為其初始值,並且反應性預設會擴展到嵌套的物件和陣列。可以將 store 視為一個多值信號或由多個信號組成的物件。
建立一個計數器元件
⚛️ 從 React 轉換
export function Counter() {
const [count, setCount] = useState(0);
return (
<>
<p>Value is: {count}</p>
<button onClick={() => setCount(count + 1)}>Increment</button>
</>
);
}
⚡️ 轉換至 Qwik
export const Counter = component$(() => {
const count = useSignal(0);
return (
<>
<p>Value is: {count.value}</p>
<button onClick$={() => count.value++}>Increment</button>
</>
);
});
使用 Props
⚛️ 從 React 轉換
export const Parent = (() => {
const [ count, setCount ] = useState<number>(0);
const increment = (() => {
setCount((prev) => prev + 1)
})
return <Child count={count} increment={increment} />;
})
interface ChildProps {
count: number;
increment: () => void;
}
export const Child = ((props: ChildProps) => {
return (
<>
<button onClick={props.increment}>Increment</button>
<p>Count: {props.count}</p>
</>
);
})
⚡️ 轉換至 Qwik
export const Parent = component$(() => {
const userData = useStore({ count: 0 });
return <Child userData={userData} />;
});
interface ChildProps {
userData: { count: number };
}
export const Child = component$<ChildProps>(({ userData }) => {
return (
<>
<button onClick$={() => userData.count++}>Increment</button>
<p>Count: {userData.count}</p>
</>
);
});
等等,我不需要將處理程式作為 prop 傳遞嗎?
useSignal() 返回的反應式信號由一個物件組成,該物件具有一個 .value 屬性。如果您更改信號的 value 屬性,則任何依賴於它的元件都將自動更新。
建立一個每秒遞增的時鐘
⚛️ 從 React 轉換
export function Clock() {
const [seconds, setSeconds] = useState(0);
useEffect(() => {
const interval = setInterval(() => {
setSeconds(seconds + 1);
}, 1000);
return () => clearInterval(interval);
});
return <p>Seconds: {seconds}</p>;
}
⚡️ 轉換至 Qwik
export const Clock = component$(() => {
const seconds = useSignal(0);
useVisibleTask$(({ cleanup }) => {
const interval = setInterval(() => {
seconds.value++;
}, 1000);
cleanup(() => clearInterval(interval));
});
return <p>Seconds: {seconds.value}</p>;
});
每次狀態更改時執行提取請求
⚛️ 從 React 轉換
export function Fetch() {
const [url, setUrl] = useState('https://api.github.com/repos/QwikDev/qwik');
const [responseJson, setResponseJson] = useState(undefined);
useEffect(() => {
fetch(url)
.then((res) => res.json())
.then((json) => setResponseJson(json));
}, [url]);
return (
<>
<p>{responseJson?.name} has {responseJson?.stargazers_count} ✨'s</p>
<input name="url" value={url} onChange={(e) => setUrl(e.target.value)} />
</>
);
}
⚡️ 轉換至 Qwik
export const Fetch = component$(() => {
const url = useSignal('https://api.github.com/repos/QwikDev/qwik');
const responseJson = useSignal(undefined);
useTask$(async ({ track }) => {
track(() => url.value);
const res = await fetch(url.value);
const json = await res.json();
responseJson.value = json;
});
return (
<>
<p>{responseJson.value?.name} has {responseJson.value?.stargazers_count} ✨'s</p>
<input name="url" bind:value={url} />
</>
);
});
什麼是 useTask$ 和 bind?
useTask$() 註冊一個要在元件建立時執行的鉤子,它至少會在伺服器或瀏覽器中執行一次,具體取決於元件最初在哪裡渲染。
bind 屬性 是一個方便的 API,用於將 input
的值雙向資料綁定到信號。
宣告一些上下文並使用它
⚛️ 從 React 開始
export const MyContext = createContext({ message: 'some example value' });
export default function Parent() {
return (
<MyContext.Provider value={{ message: 'updated example value' }}>
<Child />
</MyContext.Provider>
);
}
export const Child = () => {
const value = useContext(MyContext);
return <p>{value.message}</p>;
};
⚡️ 到 Qwik
export const MyContext = createContextId('my-context');
export const Parent = component$(() => {
const message = useSignal('some example value');
useContextProvider(MyContext, message);
return (
<>
<Child />
</>
);
});
export const Child = component$(() => {
const message = useContext(MyContext);
return <p>{message.value}</p>;
});
創建一個防抖動輸入框
⚛️ 從 React 開始
export const DebouncedInput = () => {
const [value, setValue] = useState('');
const [debouncedValue, setDebouncedValue] = useState(value);
useEffect(() => {
const debounced = setTimeout(() => setDebouncedValue(value), 1000);
return () => clearTimeout(debounced);
}, [value]);
return (
<>
<input
value={value}
onChange={(ev) => setValue((ev.target as HTMLInputElement).value)}
/>
<p>{debouncedValue}</p>
</>
);
};
⚡️ 到 Qwik
export const DebouncedInput = component$(() => {
const inputText = useSignal('');
const debouncedValue = useSignal('');
useTask$(({ track, cleanup }) => {
track(() => inputText.value);
const debounced = setTimeout(() => {
debouncedValue.value = inputText.value;
}, 1000);
cleanup(() => clearTimeout(debounced));
});
return (
<>
<input bind:value={inputText} />
<p>{debouncedValue.value}</p>
</>
);
});
每次點擊按鈕時隨機更改背景顏色
⚛️ 從 React 開始
export function DynamicBackground() {
const [red, setRed] = useState(0);
const [green, setGreen] = useState(0);
const [blue, setBlue] = useState(0);
return (
<div
style={{
background: `rgb(${red}, ${green}, ${blue})`,
}}
>
<button
onClick={() => {
setRed(Math.random() * 256);
setGreen(Math.random() * 256);
setBlue(Math.random() * 256);
}}
>
Change background
</button>
</div>
);
}
⚡️ 到 Qwik
export const DynamicBackground = component$(() => {
const red = useSignal(0);
const green = useSignal(0);
const blue = useSignal(0);
return (
<div
style={{
background: `rgb(${red.value}, ${green.value}, ${blue.value})`,
}}
>
<button
onClick$={() => {
red.value = Math.random() * 256;
green.value = Math.random() * 256;
blue.value = Math.random() * 256;
}}
>
Change background
</button>
</div>
);
});
創建一個渲染總統列表的元件
⚛️ 從 React 開始
export function Presidents() {
const presidents = [
{ name: 'George Washington', years: '1789-1797' },
{ name: 'John Adams', years: '1797-1801' },
{ name: 'Thomas Jefferson', years: '1801-1809' },
{ name: 'James Madison', years: '1809-1817' },
{ name: 'James Monroe', years: '1817-1825' },
{ name: 'John Quincy Adams', years: '1825-1829' },
{ name: 'Andrew Jackson', years: '1829-1837' },
{ name: 'Martin Van Buren', years: '1837-1841' },
{ name: 'William Henry Harrison', years: '1841-1841' },
{ name: 'John Tyler', years: '1841-1845' },
{ name: 'James K. Polk', years: '1845-1849' },
{ name: 'Zachary Taylor', years: '1849-1850' },
{ name: 'Millard Fillmore', years: '1850-1853' },
{ name: 'Franklin Pierce', years: '1853-1857' },
{ name: 'James Buchanan', years: '1857-1861' },
{ name: 'Abraham Lincoln', years: '1861-1865' },
{ name: 'Andrew Johnson', years: '1865-1869' },
{ name: 'Ulysses S. Grant', years: '1869-1877' },
{ name: 'Rutherford B. Hayes', years: '1877-1881' },
{ name: 'James A. Garfield', years: '1881-1881' },
{ name: 'Chester A. Arthur', years: '1881-1885' },
{ name: 'Grover Cleveland', years: '1885-1889' },
];
return (
<ul>
{presidents.map((president) => (
<li key={president.name + president.years}>
{president.name} ({president.years})
</li>
))}
</ul>
);
}
⚡️ 到 Qwik
export const Presidents = component$(() => {
const presidents = [
{ name: 'George Washington', years: '1789-1797' },
{ name: 'John Adams', years: '1797-1801' },
{ name: 'Thomas Jefferson', years: '1801-1809' },
{ name: 'James Madison', years: '1809-1817' },
{ name: 'James Monroe', years: '1817-1825' },
{ name: 'John Quincy Adams', years: '1825-1829' },
{ name: 'Andrew Jackson', years: '1829-1837' },
{ name: 'Martin Van Buren', years: '1837-1841' },
{ name: 'William Henry Harrison', years: '1841-1841' },
{ name: 'John Tyler', years: '1841-1845' },
{ name: 'James K. Polk', years: '1845-1849' },
{ name: 'Zachary Taylor', years: '1849-1850' },
{ name: 'Millard Fillmore', years: '1850-1853' },
{ name: 'Franklin Pierce', years: '1853-1857' },
{ name: 'James Buchanan', years: '1857-1861' },
{ name: 'Abraham Lincoln', years: '1861-1865' },
{ name: 'Andrew Johnson', years: '1865-1869' },
{ name: 'Ulysses S. Grant', years: '1869-1877' },
{ name: 'Rutherford B. Hayes', years: '1877-1881' },
{ name: 'James A. Garfield', years: '1881-1881' },
{ name: 'Chester A. Arthur', years: '1881-1885' },
{ name: 'Grover Cleveland', years: '1885-1889' },
];
return (
<ul>
{presidents.map((president) => (
<li key={president.name + president.years}>
{president.name} ({president.years})
</li>
))}
</ul>
);
});