Fix: How to use forwardRef?#227
Conversation
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request enhances the guide by providing a clear and practical example of how to correctly implement and use Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
The pull request introduces a new section on 'Forwarding Refs' in React, including a new FancyButton component demonstrating React.forwardRef and its usage with React.createRef. However, a critical issue was identified where the README_SOURCE.md file was entirely overwritten with this new content, inadvertently deleting the existing guide. The README_SOURCE.md needs to be restored, and the new section should be properly integrated into the existing documentation.
| ### Forwarding Refs | ||
|
|
||
| # React & Redux in TypeScript - Complete Guide | ||
| > https://reactjs.org/docs/forwarding-refs.html | ||
|
|
||
| _"This guide is a **living compendium** documenting the most important patterns and recipes on how to use **React** (and its Ecosystem) in a **functional style** using **TypeScript**. It will help you make your code **completely type-safe** while focusing on **inferring the types from implementation** so there is less noise coming from excessive type annotations and it's easier to write and maintain correct types in the long run."_ | ||
| > #### `React.forwardRef` - Forward ref to a DOM element | ||
|
|
||
| [](https://spectrum.chat/react-redux-ts) | ||
| [](https://gitter.im/react-redux-typescript-guide/Lobby) | ||
|
|
||
| _Found it useful? Want more updates?_ | ||
|
|
||
| [**Show your support by giving a :star:**](https://github.com/piotrwitek/react-redux-typescript-guide/stargazers) | ||
|
|
||
| <a href="https://www.buymeacoffee.com/piotrekwitek"> | ||
| <img src="https://www.buymeacoffee.com/assets/img/custom_images/orange_img.png" alt="Buy Me a Coffee"> | ||
| </a> | ||
| <a href="https://www.patreon.com/piotrekwitek"> | ||
| <img src="https://c5.patreon.com/external/logo/become_a_patron_button@2x.png" alt="Become a Patron" width="160"> | ||
| </a> | ||
|
|
||
| <br/><hr/> | ||
|
|
||
| ## **What's new?** | ||
|
|
||
| :tada: _Now updated to support **TypeScript v4.6**_ :tada: | ||
| :rocket: _Updated to `typesafe-actions@5.x` :rocket: | ||
|
|
||
| <hr/><br/> | ||
|
|
||
| </div> | ||
|
|
||
| ### **Goals** | ||
|
|
||
| - Complete type safety (with [`--strict`](https://www.typescriptlang.org/docs/handbook/compiler-options.html) flag) without losing type information downstream through all the layers of our application (e.g. no type assertions or hacking with `any` type) | ||
| - Make type annotations concise by eliminating redundancy in types using advanced TypeScript Language features like **Type Inference** and **Control flow analysis** | ||
| - Reduce repetition and complexity of types with TypeScript focused [complementary libraries](#react-redux-typescript-ecosystem) | ||
|
|
||
| ### **React, Redux, Typescript Ecosystem** | ||
|
|
||
| - [typesafe-actions](https://github.com/piotrwitek/typesafe-actions) - Typesafe utilities for "action-creators" in Redux / Flux Architecture | ||
| - [utility-types](https://github.com/piotrwitek/utility-types) - Collection of generic types for TypeScript, complementing built-in mapped types and aliases - think lodash for reusable types. | ||
| - [react-redux-typescript-scripts](https://github.com/piotrwitek/react-redux-typescript-scripts) - dev-tools configuration files shared between projects based on this guide | ||
|
|
||
| ### **Examples** | ||
|
|
||
| - Todo-App playground: [Codesandbox](https://codesandbox.io/s/github/piotrwitek/typesafe-actions/tree/master/codesandbox) | ||
| - React, Redux, TypeScript - RealWorld App: [Github](https://github.com/piotrwitek/react-redux-typescript-realworld-app) | [Demo](https://react-redux-typescript-realworld-app.netlify.com/) | ||
|
|
||
| ### **Playground Project** | ||
|
|
||
| [](https://semaphoreci.com/piotrekwitek/react-redux-typescript-guide) | ||
|
|
||
| Check out our Playground Project located in the `/playground` folder. It contains all source files of the code examples found in the guide. They are all tested with the most recent version of TypeScript and 3rd party type-definitions (like `@types/react` or `@types/react-redux`) to ensure the examples are up-to-date and not broken with updated definitions (It's based on `create-react-app --typescript`). | ||
| > Playground project was created so that you can simply clone the repository locally and immediately play around with all the component patterns found in the guide. It will help you to learn all the examples from this guide in a real project environment without the need to create complicated environment setup by yourself. | ||
|
|
||
| ## Contributing Guide | ||
|
|
||
| You can help make this project better by contributing. If you're planning to contribute please make sure to check our contributing guide: [CONTRIBUTING.md](/CONTRIBUTING.md) | ||
|
|
||
| ## Funding | ||
|
|
||
| You can also help by funding issues. | ||
| Issues like bug fixes or feature requests can be very quickly resolved when funded through the IssueHunt platform. | ||
|
|
||
| I highly recommend to add a bounty to the issue that you're waiting for to increase priority and attract contributors willing to work on it. | ||
|
|
||
| [](https://issuehunt.io/repos/76996763) | ||
|
|
||
| --- | ||
|
|
||
| 🌟 - _New or updated section_ | ||
|
|
||
| ## Table of Contents | ||
|
|
||
| <!-- START doctoc generated TOC please keep comment here to allow auto update --> | ||
| <!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE --> | ||
|
|
||
|
|
||
| - [React Types Cheatsheet](#react-types-cheatsheet) | ||
| - [`React.FC<Props>` | `React.FunctionComponent<Props>`](#reactfcprops--reactfunctioncomponentprops) | ||
| - [`React.Component<Props, State>`](#reactcomponentprops-state) | ||
| - [`React.ComponentType<Props>`](#reactcomponenttypeprops) | ||
| - [`React.ComponentProps<typeof XXX>`](#reactcomponentpropstypeof-xxx) | ||
| - [`React.ReactElement` | `JSX.Element`](#reactreactelement--jsxelement) | ||
| - [`React.ReactNode`](#reactreactnode) | ||
| - [`React.CSSProperties`](#reactcssproperties) | ||
| - [`React.XXXHTMLAttributes<HTMLXXXElement>`](#reactxxxhtmlattributeshtmlxxxelement) | ||
| - [`React.ReactEventHandler<HTMLXXXElement>`](#reactreacteventhandlerhtmlxxxelement) | ||
| - [`React.XXXEvent<HTMLXXXElement>`](#reactxxxeventhtmlxxxelement) | ||
| - [React](#react) | ||
| - [Function Components - FC](#function-components---fc) | ||
| - [- Counter Component](#--counter-component) | ||
| - [- Counter Component with default props](#--counter-component-with-default-props) | ||
| - [- Spreading attributes in Component](#--spreading-attributes-in-component) | ||
| - [Class Components](#class-components) | ||
| - [- Class Counter Component](#--class-counter-component) | ||
| - [- Class Component with default props](#--class-component-with-default-props) | ||
| - [Generic Components](#generic-components) | ||
| - [- Generic List Component](#--generic-list-component) | ||
| - [Hooks](#hooks) | ||
| - [- useState](#--usestate) | ||
| - [- useContext](#--usecontext) | ||
| - [- useReducer](#--usereducer) | ||
| - [Render Props](#render-props) | ||
| - [- Name Provider Component](#--name-provider-component) | ||
| - [- Mouse Provider Component](#--mouse-provider-component) | ||
| - [Higher-Order Components](#higher-order-components) | ||
| - [- HOC wrapping a component](#--hoc-wrapping-a-component) | ||
| - [- HOC wrapping a component and injecting props](#--hoc-wrapping-a-component-and-injecting-props) | ||
| - [- Nested HOC - wrapping a component, injecting props and connecting to redux 🌟](#--nested-hoc---wrapping-a-component-injecting-props-and-connecting-to-redux-) | ||
| - [Redux Connected Components](#redux-connected-components) | ||
| - [- Redux connected counter](#--redux-connected-counter) | ||
| - [- Redux connected counter with own props](#--redux-connected-counter-with-own-props) | ||
| - [- Redux connected counter via hooks](#--redux-connected-counter-via-hooks) | ||
| - [- Redux connected counter with `redux-thunk` integration](#--redux-connected-counter-with-redux-thunk-integration) | ||
| - [Context](#context) | ||
| - [ThemeContext](#themecontext) | ||
| - [ThemeProvider](#themeprovider) | ||
| - [ThemeConsumer](#themeconsumer) | ||
| - [ThemeConsumer in class component](#themeconsumer-in-class-component) | ||
| - [Redux](#redux) | ||
| - [Store Configuration](#store-configuration) | ||
| - [Create Global Store Types](#create-global-store-types) | ||
| - [Create Store](#create-store) | ||
| - [Action Creators 🌟](#action-creators-) | ||
| - [Reducers](#reducers) | ||
| - [State with Type-level Immutability](#state-with-type-level-immutability) | ||
| - [Typing reducer](#typing-reducer) | ||
| - [Typing reducer with `typesafe-actions`](#typing-reducer-with-typesafe-actions) | ||
| - [Testing reducer](#testing-reducer) | ||
| - [Async Flow with `redux-observable`](#async-flow-with-redux-observable) | ||
| - [Typing epics](#typing-epics) | ||
| - [Testing epics](#testing-epics) | ||
| - [Selectors with `reselect`](#selectors-with-reselect) | ||
| - [Connect with `react-redux`](#connect-with-react-redux) | ||
| - [Typing connected component](#typing-connected-component) | ||
| - [Typing `useSelector` and `useDispatch`](#typing-useselector-and-usedispatch) | ||
| - [Typing connected component with `redux-thunk` integration](#typing-connected-component-with-redux-thunk-integration) | ||
| - [Configuration & Dev Tools](#configuration--dev-tools) | ||
| - [Common Npm Scripts](#common-npm-scripts) | ||
| - [tsconfig.json](#tsconfigjson) | ||
| - [TSLib](#tslib) | ||
| - [ESLint](#eslint) | ||
| - [.eslintrc.js](#eslintrcjs) | ||
| - [Jest](#jest) | ||
| - [jest.config.json](#jestconfigjson) | ||
| - [jest.stubs.js](#jeststubsjs) | ||
| - [Style Guides](#style-guides) | ||
| - [react-styleguidist](#react-styleguidist) | ||
| - [FAQ](#faq) | ||
| - [Ambient Modules](#ambient-modules) | ||
| - [Imports in ambient modules](#imports-in-ambient-modules) | ||
| - [Type-Definitions](#type-definitions) | ||
| - [Missing type-definitions error](#missing-type-definitions-error) | ||
| - [Using custom `d.ts` files for npm modules](#using-custom-dts-files-for-npm-modules) | ||
| - [Type Augmentation](#type-augmentation) | ||
| - [Augmenting library internal declarations - using relative import](#augmenting-library-internal-declarations---using-relative-import) | ||
| - [Augmenting library public declarations - using node_modules import](#augmenting-library-public-declarations---using-node_modules-import) | ||
| - [Misc](#misc) | ||
| - [- should I still use React.PropTypes in TS?](#--should-i-still-use-reactproptypes-in-ts) | ||
| - [- when to use `interface` declarations and when `type` aliases?](#--when-to-use-interface-declarations-and-when-type-aliases) | ||
| - [- what's better default or named exports?](#--whats-better-default-or-named-exports) | ||
| - [- how to best initialize class instance or static properties?](#--how-to-best-initialize-class-instance-or-static-properties) | ||
| - [- how to best declare component handler functions?](#--how-to-best-declare-component-handler-functions) | ||
| - [Tutorials & Articles](#tutorials--articles) | ||
| - [Contributors](#contributors) | ||
|
|
||
| <!-- END doctoc generated TOC please keep comment here to allow auto update --> | ||
|
|
||
| --- | ||
|
|
||
| # Installation | ||
|
|
||
| ## Types for React & Redux | ||
|
|
||
| ``` | ||
| npm i -D @types/react @types/react-dom @types/react-redux | ||
| ``` | ||
|
|
||
| "react" - `@types/react` | ||
| "react-dom" - `@types/react-dom` | ||
| "redux" - (types included with npm package)* | ||
| "react-redux" - `@types/react-redux` | ||
|
|
||
| > *NB: Guide is based on types for Redux >= v4.x.x. | ||
|
|
||
| [⇧ back to top](#table-of-contents) | ||
|
|
||
| --- | ||
|
|
||
| ## React Types Cheatsheet | ||
|
|
||
| ### `React.FC<Props>` | `React.FunctionComponent<Props>` | ||
|
|
||
| Type representing a functional component | ||
|
|
||
| ```tsx | ||
| const MyComponent: React.FC<Props> = ... | ||
| ``` | ||
|
|
||
| ### `React.Component<Props, State>` | ||
|
|
||
| Type representing a class component | ||
|
|
||
| ```tsx | ||
| class MyComponent extends React.Component<Props, State> { ... | ||
| ``` | ||
|
|
||
| ### `React.ComponentType<Props>` | ||
|
|
||
| Type representing union of (`React.FC<Props> | React.Component<Props>`) - used in HOC | ||
|
|
||
| ```tsx | ||
| const withState = <P extends WrappedComponentProps>( | ||
| WrappedComponent: React.ComponentType<P>, | ||
| ) => { ... | ||
| ``` | ||
|
|
||
| ### `React.ComponentProps<typeof XXX>` | ||
|
|
||
| Gets Props type of a specified component XXX (WARNING: does not work with statically declared default props and generic props) | ||
|
|
||
| ```tsx | ||
| type MyComponentProps = React.ComponentProps<typeof MyComponent>; | ||
| ``` | ||
|
|
||
| ### `React.ReactElement` | `JSX.Element` | ||
|
|
||
| Type representing a concept of React Element - representation of a native DOM component (e.g. `<div />`), or a user-defined composite component (e.g. `<MyComponent />`) | ||
|
|
||
| ```tsx | ||
| const elementOnly: React.ReactElement = <div /> || <MyComponent />; | ||
| ``` | ||
|
|
||
| ### `React.ReactNode` | ||
|
|
||
| Type representing any possible type of React node (basically ReactElement (including Fragments and Portals) + primitive JS types) | ||
|
|
||
| ```tsx | ||
| const elementOrPrimitive: React.ReactNode = 'string' || 0 || false || null || undefined || <div /> || <MyComponent />; | ||
| const Component = ({ children: React.ReactNode }) => ... | ||
| ``` | ||
|
|
||
| ### `React.CSSProperties` | ||
|
|
||
| Type representing style object in JSX - for css-in-js styles | ||
|
|
||
| ```tsx | ||
| const styles: React.CSSProperties = { flexDirection: 'row', ... | ||
| const element = <div style={styles} ... | ||
| ``` | ||
|
|
||
| ### `React.XXXHTMLAttributes<HTMLXXXElement>` | ||
|
|
||
| Type representing HTML attributes of specified HTML Element - for extending HTML Elements | ||
|
|
||
| ```tsx | ||
| const Input: React.FC<Props & React.InputHTMLAttributes<HTMLInputElement>> = props => { ... } | ||
|
|
||
| <Input about={...} accept={...} alt={...} ... /> | ||
| ``` | ||
|
|
||
| ### `React.ReactEventHandler<HTMLXXXElement>` | ||
|
|
||
| Type representing generic event handler - for declaring event handlers | ||
|
|
||
| ```tsx | ||
| const handleChange: React.ReactEventHandler<HTMLInputElement> = (ev) => { ... } | ||
|
|
||
| <input onChange={handleChange} ... /> | ||
| ``` | ||
|
|
||
| ### `React.XXXEvent<HTMLXXXElement>` | ||
|
|
||
| Type representing more specific event. Some common event examples: `ChangeEvent, FormEvent, FocusEvent, KeyboardEvent, MouseEvent, DragEvent, PointerEvent, WheelEvent, TouchEvent`. | ||
|
|
||
| ```tsx | ||
| const handleChange = (ev: React.MouseEvent<HTMLDivElement>) => { ... } | ||
|
|
||
| <div onMouseMove={handleChange} ... /> | ||
| ``` | ||
|
|
||
| In code above `React.MouseEvent<HTMLDivElement>` is type of mouse event, and this event happened on `HTMLDivElement` | ||
|
|
||
| [⇧ back to top](#table-of-contents) | ||
|
|
||
| --- | ||
|
|
||
| # React | ||
|
|
||
| ## Function Components - FC | ||
|
|
||
| ### - Counter Component | ||
|
|
||
| ::codeblock='playground/src/components/fc-counter.tsx':: | ||
|
|
||
| [⟩⟩⟩ demo](https://piotrwitek.github.io/react-redux-typescript-guide/#fccounter) | ||
|
|
||
| [⇧ back to top](#table-of-contents) | ||
|
|
||
| ### - Counter Component with default props | ||
|
|
||
| ::codeblock='playground/src/components/fc-counter-with-default-props.tsx':: | ||
|
|
||
| [⟩⟩⟩ demo](https://piotrwitek.github.io/react-redux-typescript-guide/#fccounterwithdefaultprops) | ||
|
|
||
| [⇧ back to top](#table-of-contents) | ||
|
|
||
| ### - [Spreading attributes](https://facebook.github.io/react/docs/jsx-in-depth.html#spread-attributes) in Component | ||
|
|
||
| ::codeblock='playground/src/components/fc-spread-attributes.tsx':: | ||
|
|
||
| [⟩⟩⟩ demo](https://piotrwitek.github.io/react-redux-typescript-guide/#fcspreadattributes) | ||
|
|
||
| [⇧ back to top](#table-of-contents) | ||
|
|
||
| --- | ||
|
|
||
| ## Class Components | ||
|
|
||
| ### - Class Counter Component | ||
|
|
||
| ::codeblock='playground/src/components/class-counter.tsx':: | ||
|
|
||
| [⟩⟩⟩ demo](https://piotrwitek.github.io/react-redux-typescript-guide/#classcounter) | ||
|
|
||
| [⇧ back to top](#table-of-contents) | ||
|
|
||
| ### - Class Component with default props | ||
|
|
||
| ::codeblock='playground/src/components/class-counter-with-default-props.tsx':: | ||
|
|
||
| [⟩⟩⟩ demo](https://piotrwitek.github.io/react-redux-typescript-guide/#classcounterwithdefaultprops) | ||
|
|
||
| [⇧ back to top](#table-of-contents) | ||
|
|
||
| --- | ||
|
|
||
| ## Generic Components | ||
|
|
||
| - easily create typed component variations and reuse common logic | ||
| - common use case is a generic list components | ||
|
|
||
| ### - Generic List Component | ||
|
|
||
| ::codeblock='playground/src/components/generic-list.tsx':: | ||
|
|
||
| [⟩⟩⟩ demo](https://piotrwitek.github.io/react-redux-typescript-guide/#genericlist) | ||
|
|
||
| [⇧ back to top](#table-of-contents) | ||
|
|
||
| --- | ||
|
|
||
| ## Hooks | ||
|
|
||
| > <https://reactjs.org/docs/hooks-intro.html> | ||
|
|
||
| ### - useState | ||
|
|
||
| > <https://reactjs.org/docs/hooks-reference.html#usestate> | ||
|
|
||
| ::codeblock='playground/src/hooks/use-state.tsx':: | ||
|
|
||
| [⇧ back to top](#table-of-contents) | ||
|
|
||
| ### - useContext | ||
|
|
||
| > <https://reactjs.org/docs/hooks-reference.html#usecontext> | ||
|
|
||
| ::codeblock='playground/src/hooks/use-theme-context.tsx':: | ||
|
|
||
| [⇧ back to top](#table-of-contents) | ||
|
|
||
| ### - useReducer | ||
|
|
||
| > <https://reactjs.org/docs/hooks-reference.html#usereducer> | ||
|
|
||
| ::codeblock='playground/src/hooks/use-reducer.tsx':: | ||
|
|
||
| [⇧ back to top](#table-of-contents) | ||
|
|
||
| --- | ||
|
|
||
| ## Render Props | ||
|
|
||
| > <https://reactjs.org/docs/render-props.html> | ||
|
|
||
| ### - Name Provider Component | ||
|
|
||
| Simple component using children as a render prop | ||
|
|
||
| ::codeblock='playground/src/components/name-provider.tsx':: | ||
|
|
||
| [⟩⟩⟩ demo](https://piotrwitek.github.io/react-redux-typescript-guide/#nameprovider) | ||
|
|
||
| [⇧ back to top](#table-of-contents) | ||
|
|
||
| ### - Mouse Provider Component | ||
|
|
||
| `Mouse` component found in [Render Props React Docs](https://reactjs.org/docs/render-props.html#use-render-props-for-cross-cutting-concerns) | ||
|
|
||
| ::codeblock='playground/src/components/mouse-provider.tsx':: | ||
|
|
||
| [⟩⟩⟩ demo](https://piotrwitek.github.io/react-redux-typescript-guide/#mouseprovider) | ||
|
|
||
| [⇧ back to top](#table-of-contents) | ||
|
|
||
| --- | ||
|
|
||
| ## Higher-Order Components | ||
|
|
||
| > <https://reactjs.org/docs/higher-order-components.html> | ||
|
|
||
| ### - HOC wrapping a component | ||
|
|
||
| Adds state to a stateless counter | ||
|
|
||
| ::codeblock='playground/src/hoc/with-state.tsx':: | ||
| ::expander='playground/src/hoc/with-state.usage.tsx':: | ||
|
|
||
| [⇧ back to top](#table-of-contents) | ||
|
|
||
| ### - HOC wrapping a component and injecting props | ||
|
|
||
| Adds error handling using componentDidCatch to any component | ||
|
|
||
| ::codeblock='playground/src/hoc/with-error-boundary.tsx':: | ||
| ::expander='playground/src/hoc/with-error-boundary.usage.tsx':: | ||
|
|
||
| [⇧ back to top](#table-of-contents) | ||
|
|
||
| ### - Nested HOC - wrapping a component, injecting props and connecting to redux 🌟 | ||
|
|
||
| Adds error handling using componentDidCatch to any component | ||
|
|
||
| ::codeblock='playground/src/hoc/with-connected-count.tsx':: | ||
| ::expander='playground/src/hoc/with-connected-count.usage.tsx':: | ||
|
|
||
| [⇧ back to top](#table-of-contents) | ||
|
|
||
| --- | ||
|
|
||
| ## Redux Connected Components | ||
|
|
||
| ### - Redux connected counter | ||
|
|
||
| ::codeblock='playground/src/connected/fc-counter-connected.tsx':: | ||
| ::expander='playground/src/connected/fc-counter-connected.usage.tsx':: | ||
|
|
||
| [⇧ back to top](#table-of-contents) | ||
|
|
||
| ### - Redux connected counter with own props | ||
|
|
||
| ::codeblock='playground/src/connected/fc-counter-connected-own-props.tsx':: | ||
| ::expander='playground/src/connected/fc-counter-connected-own-props.usage.tsx':: | ||
|
|
||
| [⇧ back to top](#table-of-contents) | ||
|
|
||
| ### - Redux connected counter via hooks | ||
|
|
||
| ::codeblock='playground/src/hooks/react-redux-hooks.tsx':: | ||
|
|
||
| [⇧ back to top](#table-of-contents) | ||
|
|
||
| ### - Redux connected counter with `redux-thunk` integration | ||
|
|
||
| ::codeblock='playground/src/connected/fc-counter-connected-bind-action-creators.tsx':: | ||
| ::expander='playground/src/connected/fc-counter-connected-bind-action-creators.usage.tsx':: | ||
|
|
||
| [⇧ back to top](#table-of-contents) | ||
|
|
||
| ## Context | ||
|
|
||
| > <https://reactjs.org/docs/context.html> | ||
|
|
||
| ### ThemeContext | ||
|
|
||
| ::codeblock='playground/src/context/theme-context.ts':: | ||
|
|
||
| [⇧ back to top](#table-of-contents) | ||
|
|
||
| ### ThemeProvider | ||
|
|
||
| ::codeblock='playground/src/context/theme-provider.tsx':: | ||
|
|
||
| [⇧ back to top](#table-of-contents) | ||
|
|
||
| ### ThemeConsumer | ||
|
|
||
| ::codeblock='playground/src/context/theme-consumer.tsx':: | ||
|
|
||
| ### ThemeConsumer in class component | ||
|
|
||
| ::codeblock='playground/src/context/theme-consumer-class.tsx':: | ||
|
|
||
| [Implementation with Hooks](#--usecontext) | ||
|
|
||
| [⇧ back to top](#table-of-contents) | ||
|
|
||
|
|
||
| --- | ||
|
|
||
| # Redux | ||
|
|
||
| ## Store Configuration | ||
|
|
||
| ### Create Global Store Types | ||
|
|
||
| #### `RootState` - type representing root state-tree | ||
|
|
||
| Can be imported in connected components to provide type-safety to Redux `connect` function | ||
|
|
||
| #### `RootAction` - type representing union type of all action objects | ||
|
|
||
| Can be imported in various layers receiving or sending redux actions like: reducers, sagas or redux-observables epics | ||
|
|
||
| ::codeblock='playground/src/store/types.d.ts':: | ||
| ::codeblock='playground/src/components/fc-forward-ref.tsx':: | ||
|
|
||
| [⇧ back to top](#table-of-contents) |
There was a problem hiding this comment.
It appears that the entire content of README_SOURCE.md has been replaced with this new section on Forwarding Refs. This has resulted in the removal of the entire guide. Based on your pull request description, the intention was likely to add this section to the existing file, not to replace it. Please restore the original content of the file and insert this new section in the appropriate place.
Summary
This PR addresses: #72
Task: How to use forwardRef?
Platform: github
Changes
Looking at this issue, I need to add documentation and example code showing how to use
React.forwardRefwith TypeScript in the react-redux-typescript-guide.Let me analyze the existing patterns in the codebase to match the style, then create the appropriate files.
Solution
I'll create a new playground component demonstrating
forwardRefusage and update the README source to include it.Now, here is how the README_SOURCE.md section should be added (showing where to insert the
forwardRefsection):Explanation
The correct way to type a component using
React.forwardRefis: