react

package
v0.0.0-...-786626a Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 1, 2026 License: BSD-2-Clause Imports: 2 Imported by: 0

Documentation

Overview

Package react provides basic React bindings for GopherJS. This is not an exhaustive React binding, but provides enough functionality to create React components and use React hooks needed by the playground application.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrReactDOMClientNotLoaded = errors.New(`react: ReactDOMClient is not loaded`)
	ErrReactNotLoaded          = errors.New(`react: React is not loaded`)
	ErrUndefinedPropKey        = errors.New(`react: undefined prop key`)
	ErrUninitializedValueRef   = errors.New(`react: uninitialized value ref`)
	ErrMissingGoValue          = errors.New(`react: missing value in go value registry`)
)
View Source
var (
	ReactDOMClient *js.Object
	React          *js.Object
)

Functions

func GetProp

func GetProp[T any](p Props, key string) T

func Memo

func Memo(component any) any

Memo prevents a component from re-rendering when its props haven't changed. Without this, every child re-renders whenever the parent renders, even if nothing relevant changed.

See: https://react.dev/reference/react/memo

func UseContext

func UseContext[T any](ctx Context[T]) T

UseContext is called from a component to looks up the information for the context that wraps that component.

See: https://react.dev/reference/react/useContext

func UseEffect

func UseEffect(effect func(), deps []any)

UseEffect registers an effect function that is called after rendering. The effect is re-run whenever any of the dependencies change.

If nil is given for the dependencies, the effect is re-run after every render. If an empty `[]any{}` is given for the dependencies, the effect is only run once after the initial render. Setters, Funcs, and Refs should be stablized so should normally not need to be used as dependencies. The number and order of dependencies must remain consistent.

See: https://react.dev/reference/react/useEffect

func UseEffectWithCleanup

func UseEffectWithCleanup(effect func() func(), deps []any)

UseEffectWithCleanup registers an effect function that is called after rendering. The effect is re-run whenever any of the dependencies change.

If nil is given for the dependencies, the effect is re-run after every render. If an empty `[]any{}` is given for the dependencies, the effect is only run once after the initial render. Setters, Funcs, and Refs should be stablized so should normally not need to be used as dependencies. The number and order of dependencies must remain consistent.

The effect function returns a cleanup function that is called before the effect is re-run or when the component is unmounted. See: https://react.dev/reference/react/useEffect

func UseId

func UseId() string

UseID generates a stable unique ID for accessibility attributes.

Do not call useId to generate keys in a list.

See: https://react.dev/reference/react/useId

func UseImperativeHandle

func UseImperativeHandle[T any](ref ValueRef[T], createHandle func() T, deps []any)

UseImperativeHandle exposes an imperative handle to a parent component via a ref passed as a prop. The createHandle function is called to produce the handle value, and it is re-created whenever deps change. On unmount (or before re-creation), the handle is cleared to the zero value.

Typically the handle type is an interface that a component will populate with a struct that implements that interface. This provides the parent component with a way to call into the child component to perform specific events in a much more conveniant way than using a Ref alone.

If nil is given for the dependencies, createHandle is re-run after every render. If an empty `[]any{}` is given for the dependencies, createHandle is only run once after the initial render. Setters, Funcs, and Refs should be stablized so should normally not need to be used as dependencies. The number and order of dependencies must remain consistent.

See: https://react.dev/reference/react/useImperativeHandle

func UseLayoutEffect

func UseLayoutEffect(effect func(), deps []any)

UseLayoutEffect registers an effect function that is called before rendering. The effect is re-run whenever any of the dependencies change.

UseLayoutEffect can hurt performance. Prefer UseEffect when possible.

If nil is given for the dependencies, the effect is re-run before every render. If an empty `[]any{}` is given for the dependencies, the effect is only run once before the initial render. Setters, Funcs, and Refs should be stablized so should normally not need to be used as dependencies. The number and order of dependencies must remain consistent.

See: https://react.dev/reference/react/useLayoutEffect

func UseLayoutEffectWithCleanup

func UseLayoutEffectWithCleanup(effect func() func(), deps []any)

UseLayoutEffectWithCleanup registers an effect function that is called before rendering. The effect is re-run whenever any of the dependencies change.

UseLayoutEffectWithCleanup can hurt performance. Prefer UseEffectWithCleanup when possible.

If nil is given for the dependencies, the effect is re-run before every render. If an empty `[]any{}` is given for the dependencies, the effect is only run once before the initial render. Setters, Funcs, and Refs should be stablized so should normally not need to be used as dependencies. The number and order of dependencies must remain consistent.

The effect function returns a cleanup function that is called before the effect is re-run or when the component is unmounted. See: https://react.dev/reference/react/useLayoutEffect

func UseMemo

func UseMemo[T any](compute func() T, deps []any) T

UseMemo memoizes the result of a computation function. The computation is only re-run when any of the dependencies change.

If nil is given for the dependencies, the computation is re-run for every render. If an empty `[]any{}` is given for the dependencies, the computation is only run once for the initial render. Setters, Funcs, and Refs should be stablized so should normally not need to be used as dependencies. The number and order of dependencies must remain consistent.

See: https://react.dev/reference/react/useMemo

func UseSyncExternalStore

func UseSyncExternalStore[T any](subscribe func(onStoreChange func()) func(), getSnapshot func() T) T

UseSyncExternalStore subscribes to an external store and returns its current snapshot. React will call getSnapshot to read the current value and re-render whenever subscribe's onStoreChange callback is called.

subscribe is called with an onStoreChange callback and must return an unsubscribe function. If a different subscribe function is passed during a re-render, React will re-subscribe to the store using the newly passed subscribe function.

getSnapshot must return the current store value. The store snapshot returned by getSnapshot must be immutable.

T must be a type that survives JS round-tripping (string, int, float64, bool, *js.Object) and is comparable via Object.is(). Go types such as interfaces and structs may not be used.

See: https://react.dev/reference/react/useSyncExternalStore

Types

type Context

type Context[T any] struct{ *js.Object }

Context is a context component creater.

See: https://react.dev/reference/react/createContext

func CreateContext

func CreateContext[T any](defaultValue T) Context[T]

CreateContext lets you create a context that components can provide or read. Call CreateContext outside of any components to create a context. This allows context to be passed into child components without having to pass the context as a prop.

T must be a type that survives JS round-tripping (string, int, float64, bool, *js.Object)

See: https://react.dev/reference/react/createContext

func (Context[T]) Provider

func (c Context[T]) Provider(value T, children ...Node) *Element

Provider creates the component element for the given context.

See: https://react.dev/reference/react/createContext#provider

type Element

type Element struct{ *js.Object }

Element is a React element created with CreateElement(). It represents a UI component and can be rendered into the DOM. See: https://react.dev/reference/react/createElement

func A

func A(href string, props Props, children ...Node) *Element

A creates an `<a>` link element for a hyperlink tag.

func Button

func Button(id, value string, props Props, onClick Func) *Element

Button creates a button input element with the given value, properties, and onClick handler. The onClick handler is added to the props as the `onClick` property and is called when the button is clicked.

func CreateElement

func CreateElement(typ any, props any, children ...Node) *Element

CreateElement creates a React element of the given type with the given props and children.

The type can be a string for HTML elements (e.g. `div`, `span`, etc.), or a React component (function or class). The props can be nil if there are no properties to set, a Props map, or a struct for typed props. Children can be zero or more React nodes to be nested inside the element. See: https://react.dev/reference/react/createElement

func Div

func Div(props Props, children ...Node) *Element

func Fragment

func Fragment(children ...Node) *Element

Fragments groups items without a wrapping element. The Fragments "disappears" from the DOM such that only its children are rendered as siblings to the Fragment's sibling elements. See: https://react.dev/reference/react/Fragment

func Pre

func Pre(props Props, children ...Node) *Element

func Span

func Span(props Props, children ...Node) *Element

func StrictMode

func StrictMode(children ...Node) *Element

StrictMode lets you find common bugs in your components early during development. See: https://react.dev/reference/react/StrictMode

func TextArea

func TextArea(props Props, children ...Node) *Element

type Func

type Func interface{ Invoke(...any) *js.Object }

Func is the type of function that is passed back when a function is passed as a prop to a React element. This is because of how functions are represented in GopherJS.

See: https://github.com/gopherjs/gopherjs/blob/master/js/js.go

func UseCallback

func UseCallback(fn any, deps []any) Func

UseCallback memoizes a callback function so it maintains stable identity across renders unless dependencies change.

If nil is given for the dependencies, the function is recreated for every render. If an empty `[]any{}` is given for the dependencies, the function is only created once for the initial render. Setters, Funcs, and Refs should be stablized so should normally not need to be used as dependencies. The number and order of dependencies must remain consistent.

See: https://react.dev/reference/react/useCallback

func UseState

func UseState[T any](initial T) (T, Func)

UseState creates a state for the current component render. Returns the current state value and a setter function to update the state. The setter accepts any value, which will be the new state at the next render, or a function that takes the current state and returns the new state.

Warning: The types are externalized into JS and back into Go types when going through React, meaning some types will not be able to be recovered the same as when passed in. Therefore these should typeically use bool, int, float64, string, or *js.Objects. See https://github.com/gopherjs/gopherjs/blob/master/js/js.go

It must be called unconditionally at the top level of the component function. See: https://react.dev/reference/react/useState

func UseStateLazy

func UseStateLazy[T any](initialFn func() T) (T, Func)

UseStateLazy is the same as UseState but the initial value is computed by calling the given function only once during the initial render.

Use this to avoid expensive computations on every render for the initial state value that will always be thrown away after the first render.

Warning: The types are externalized into JS and back into Go types when going through React, meaning some types will not be able to be recovered the same as when passed in. Therefore these should typeically use bool, int, float64, string, or *js.Objects. See https://github.com/gopherjs/gopherjs/blob/master/js/js.go

See: https://react.dev/reference/react/useState

type Node

type Node any

Node is a React node that can be displayed. This will usually be a react element constructed with CreateElement(), a string, a number, null, or undefined. Node may be a slice of Nodes as well.

type Props

type Props struct {
	*js.Object
}

Props represents the properties (props) passed to a React element,

The props may carry any value or function that is needed by the component to render itself and to callback to the parent. See: https://react.dev/reference/react/createElement

func (Props) Get

func (p Props) Get(key string) *js.Object

func (Props) GetBool

func (p Props) GetBool(key string) bool

func (Props) GetFloat

func (p Props) GetFloat(key string) float64

func (Props) GetFunc

func (p Props) GetFunc(key string) Func

func (Props) GetInt

func (p Props) GetInt(key string) int

func (Props) GetRef

func (p Props) GetRef(key string) Ref

func (Props) GetString

func (p Props) GetString(key string) string

func (Props) Has

func (p Props) Has(key string) bool

func (Props) Set

func (p Props) Set(key string, value any) Props

Set will set the value to the props. The returned props should be used to chain and update the props.

type Ref

type Ref struct{ *js.Object }

Ref is a React ref created with UseRef(). This is intended to hold a reference to a DOM element or a *js.Object. It is a mutable object with a `current` property that can hold any value. Modifying the `current` property does not trigger re-renders.

See: https://react.dev/reference/react/useRef

func UseRef

func UseRef() Ref

UseRef creates a mutable ref object that persists for the lifetime of the component. The ref object has a `current` property that can hold any JS object, such as references to components.

See: https://react.dev/reference/react/useRef

func (Ref) Current

func (ref Ref) Current() *js.Object

func (Ref) SetCurrent

func (ref Ref) SetCurrent(v *js.Object)

type Root

type Root struct{ *js.Object }

Root is a React root created with CreateRoot(). It is used to render React nodes into the DOM. See: https://react.dev/reference/react-dom/client/createRoot

func CreateRoot

func CreateRoot(id string) *Root

func (*Root) Render

func (r *Root) Render(n Node)

type ValueRef

type ValueRef[T any] struct{ *js.Object }

ValueRef is a React ref created with UseRefWith() that contains a value. It is a mutable object with a `current` property that can hold any value. Modifying the `current` property does not trigger re-renders.

See: https://react.dev/reference/react/useRef

func GetValueRef

func GetValueRef[T any](p Props, key string) ValueRef[T]

func UseRefLazy

func UseRefLazy[T any](initialFn func() T) ValueRef[T]

UseRefLazy is similar to UseRef except initializes the `current` property to an initial value. Since the value is being given this will use a ValueRef to hold the value instead of a general Ref.

The given initial function will usually only be called once when initializing (or twice when in strict mode).

func UseRefWith

func UseRefWith[T any](initial T) ValueRef[T]

UseRefWith is similar to UseRef except initializes the `current` property to the given initial value. Since the value is being given this will use a ValueRef to hold the value instead of a Ref.

UseRefWith will store any value type including Go values types without allowing them to be internalized and externalized.

To avoid recreating the initial value over and over again, for complex values use UseRefLazy.

See https://react.dev/reference/react/useRef#avoiding-recreating-the-ref-contents

func (ValueRef[T]) Current

func (ref ValueRef[T]) Current() T

func (ValueRef[T]) SetCurrent

func (ref ValueRef[T]) SetCurrent(v T)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL