TommosTools

contexto

API

This page describes the functions and associated components provided by the Contexto library.


createContext

const MyContext = contexto.createContext(defaultValue, { displayName?, contextId? }?);

Creates a special Context object for use with Contexto’s extended context operations.

Parameters

Returns

createContext returns a subscription context object that can be read using Contexto’s useContext and useContexts hooks, and updated using imperative updates.

The context object has a few properties:

See the React documentation on Context objects for more details on the standard operation of contexts using Providers and Consumers.


MySubscriptionContext.Provider

Wrapping components with a context Provider specifies the value of the context for the components inside.

function App() {
    // ...
    return (
        <MySubscriptionContext.Provider value={userDetails}>
            <Page/>
        </MySubscriptionContext.Provider>
    );
}

Props


MySubscriptionContext.Consumer

An alternative (legacy) way to read a context’s value. Provided only for full compatibility claims – you should use useContext instead.

function UserSummary() {
    // Legacy way (not recommended)
    return (
        <MySubscriptionContext.Consumer>
            { user =>
                <div>
                    <a href={`/user/${user.id}`}>{user.name}</a>
                    <img src={user.iconUrl} />
                </div> }
        </MySubscriptionContext.Consumer>
    );
}

Newly written code should read context values using useContext instead:

function UserSummary() {
    // Recommended way
    const user = useContext(MySubscriptionContext);
    return (
        <div>
            <a href={`/user/${user.id}`}>{user.name}</a>
            <img src={user.iconUrl} />
        </div>
    );
}

Props


createCompatibleContext

const MyCompatibleContext = contexto.createCompatibleContext(defaultValue, { displayName?, contextId? }?);

Creates a special Context object for use with Contexto’s extended context operations which is also fully compatible with the standard React context operations, including use by class components. This has performance considerations – see Interoperability for more details.

Parameters and return value are the same as for createContext.


createProxyContext

const MyProxyContext = contexto.createProxyContext(reactContext, { contextId? });

Wraps a standard React.Context object to create a special Context object suitable for use with Contexto’s useContext and useContexts hooks.

Parameters

Returns

createProxyContext returns a read-only proxy context object to allow contexts created outside the Contexto ecosystem to be used with Contexto’s consumer hooks. The return value contains a Consumer but no Provider. See Interoperability for more details.


useContext

const value = useContext(MyContext, isEqual?);

Consume and subscribe to updates of a context’s value in a function component.

Parameters

Returns

useContext returns the context value for the calling component. This is initially the latest value of the closest MyContext.Provider ancestor, or the defaultValue of the context if there is no such ancestor. The return value is updated when that Provider value changes, unless that new value isEqual to the existing return value.


useContexts

const [valueA, valueB] = useContexts([ContextA, ContextB, ...], isEqual?);
const { valueX }       = useContexts({ valueX: ContextX, ... }, isEqual?);

Consume and subscribe to updates of multiple contexts’ values in a function component.

Parameters

Returns

useContexts returns an array (or object) mapping each context in the input to the latest value of its closest corresponding context Provider. The return value is updated when any of the Provider values changes, unless the new value isEqual to the corresponding existing value.


useContextUpdate

const update = useContextUpdate(MyContext);

Prepare a function to update the value of a Contexto context.

Parameters

Returns

useContextUpdate returns a stable “imperative updater” function which updates the value of the nearest Provider for the given context. See Imperative updates.


useBridgeValue / BridgeProvider

const bridgeValue = useBridgeValue([ContextA, ContextB, ...]);
// ...
<CustomRenderer>
    <BridgeProvider value={bridgeValue} children={/* ... */} />
</CustomRenderer>

Share a context scope between different renderers.

Parameters

Returns

useBridgeValue returns an object that can be passed to a <BridgeProvider> to “bridge” one or more contexts between different renderers. React does not propagate parent contexts to child components within a different renderer, so a context bridge is required.