DOCUMENTATION: Add starter react documentation (#1888)

This commit is contained in:
Daniel Perez Alvarez
2025-01-04 16:19:49 -08:00
committed by GitHub
parent 85fa15c5a5
commit e3eb08470b
3 changed files with 34 additions and 2 deletions
+1
View File
@@ -47,6 +47,7 @@
- [Learn to program](programming/learn.md)
- [Remote API](programming/remote_api.md)
- [Game frozen or stuck?](programming/game_frozen.md)
- [React](programming/react.md)
- [Tools & Resources](help/tools_and_resources.md)
- [Changelog](changelog.md)
- [Changelog - Legacy v1](changelog-v1.md)
@@ -0,0 +1,29 @@
# How to use React in game
Since v2.7.0, Bitburner supports React and TypeScript out of the box. You can use the jsx syntax inside `.jsx` and `.tsx` files.
## Example
Use `ns.printRaw` and `ns.tprintRaw` to render React elements in the logs and terminal.
```tsx
// timer.tsx
function Timer() {
const [seconds, setSeconds] = React.useState(0);
React.useEffect(() => {
const interval = setInterval(() => {
setSeconds((seconds) => seconds + 1);
}, 1000);
return () => clearInterval(interval);
}, []);
return <div>Seconds: {seconds}</div>;
}
export async function main(ns: NS) {
ns.tail();
ns.printRaw(<Timer />);
await ns.asleep(10000);
}
```