Skip to content

tscharke/mentor-playground

Repository files navigation

Mentor Playground

This is a collection of code snippets to show the concepts of React with TypeScript in a pragmatic way.

All code snippets are available in kind of Components, Tests, (Utility) Functions, etc.

The goal is to show replicating and adaptable concepts and techniques.

Thereby, the comprehensibility and self-explanation within the code is in the foreground. It will make it possible, even for beginners, to find inspiration.

So, you need to look at the output on the console along with the code. In other words: Always have your developer tools open 😁

It's important to note that I am showing some ways and approaches here. There is no generally right or wrong way (apart from "real" mistakes πŸ˜‰).

πŸ§‘β€πŸŽ¨ Can we see something?

Yes, πŸ˜ƒ there's an executable App (at Codesandbox), that can also run locally on your machine (see below).

Keep in mind: The focus is still on the code and tests and not on a fancy UI/UX with enterprise-relevant features πŸ˜‰

πŸš€ Mission

This repository is a start and should grow πŸ’ͺ.

Feel free to post your ideas or questions in the form of issues. Often a single sentence is enough (e.g., the title of an issue).

I am happy about every pull request (PR) from you too. Regardless if it adds concepts, corrects errors, simplifies things or describes things betterβ€”whatever comes to your mind πŸ₯³.

The steps to follow to contribute to this project you can find here.

🚧 Please be aware…
Everything in this repository is a work in progress. It's constantly changing and improving. So, there is never a finished version 🀷.

πŸ’» Setup (on your machine)

# Check out the main-branch of this repository and switch into this directory
git clone git@github.com:tscharke/mentor-playground.git && cd "$(basename "$_" .git)"

# Install all dependencies
yarn install

# Start API and frontend together
yarn run start

# (Optional) Start the BookMonkey API in a separate terminal
# yarn run start:api

# (Optional) Start the development-server in a separate terminal
# yarn run start:server

🀩 After starting the development-server, the application is showing up under http://localhost:3000.

The BookMonkey API is installed locally via package.json and starts on http://localhost:4730.

πŸ‘€ What to find

Basic examples of React components to get started:

useState

useEffect

A useReducer works like useState, but uses the Reducer concept from Redux to manage state changes. A Reducer is a plain TypeScript function (state, action) => state that decides β€” based on the dispatched Action β€” whether and how the state changes.

  • UseReducerComponent.tsx – component that uses the useReducer-Hook; renders a task-list with buttons to change each task's progress (Open β†’ In progress β†’ Done)
  • reducer.ts – the Reducer function itself
  • reducer.test.ts – tests for the Reducer
  • interfaces.ts – shared type definitions

useContext

Custom Hook

  • CustomHook.tsx – a custom useFetchDataFromAPI hook that encapsulates data-fetching logic

useRef

  • UseRefHook.tsx – accessing and manipulating DOM elements with useRef

useMemo

  • UseMemo.tsx – memoising expensive computations to avoid unnecessary recalculations

useCallback

  • UseCallback.tsx – stabilising function references to prevent unnecessary re-renders

useSyncExternalStore

  • Examples of styling React components using CSS Modules and Styled Components
  • Controlled.tsx – controlled form inputs (React manages the value)
  • Uncontrolled.tsx – uncontrolled form inputs (DOM manages the value via useRef)
  • SuspenseDemo.tsx – lazy-loading components with React.Suspense and React.lazy
  • UserList.tsx – data-fetching with Suspense using a custom resource helper

Demonstrates how to offload work to a background thread using the Web Workers API:

Explains the Iterator Protocol and Generators in depth β€” with code examples and test suites:

  • Iterators: Any object that wants to be iterable must implement [Symbol.iterator](), which returns a next() function yielding { value, done } objects. Built-in types like Array and String already include this β€” so for-of works on them out of the box.
    • iterators.test.ts – shows how to use iterators, what happens without one, and how to implement your own
  • Generators: Functions declared with function* that automatically produce an iterator.
    • generators.test.ts – covers function* syntax, generator implementation, and the connection to iterators

A collection of pragmatic TypeScript/JavaScript snippets β€” most output to the browser console:

  • shallowCopy.ts – object spread creates only a shallow clone; nested objects still share references
  • scope.ts – var function scope and the classic loop + setTimeout closure pitfall
  • promises.ts – compares Promise.all, Promise.race, and Promise.allSettled
  • functions.ts – declaration vs expression vs arrow functions; static vs instance-level properties
  • reducer.ts – typed Array.prototype.reduce with inline and external reducer functions
  • asyncReducer.ts – async reduction over fetched data and why the accumulator becomes a Promise
  • modules.ts – module patterns: IIFE, CommonJS, AMD, UMD, native modules, and closure-based encapsulation
  • interfacesAndUtilityTypes.ts – index signatures and Record to model constrained and dynamic object shapes
  • usageOfInterfaces.tsx – separating public/private props to model component API boundaries
  • factorial.test.ts – iterative vs recursive factorial including boundary and error cases
  • leftPadding.test.ts – numeric left-padding across positive/negative integers, floats, and signed zero
  • invertTree.test.ts – recursive inversion of a binary tree
  • decorator.test.ts – TC39 Stage 3 decorators (class, property, method); execution timing of each decorator type
  • Examples of React's experimental use-Hook for reading resources (Promises, Context) directly inside render

🀝 You would like to contribute?

I love if you want to contribute to this project. To do so, please follow the steps described here - thank you very much β€οΈπŸ™

β€οΈπŸ™ Love & Thanks

My thanks for the photo displayed on social media goes to: Desola Lanre-Ologun

Contributors

Nik Sumeiko
Nik Sumeiko

πŸ§‘β€πŸ«

About

A collection of code pieces to show the concepts of React & TypeScript in a pragmatic way by following the source code and the tests.

Topics

Resources

License

Contributing

Stars

Watchers

Forks

Sponsor this project

 

Contributors