react-element-replace
Version:
React utility methods that transforms element subtrees, replacing elements following the provided rules.
19 lines (18 loc) • 848 B
TypeScript
import * as React from "react";
export declare type MatchFunc<S> = (value: React.ReactNode, state?: S) => boolean;
export declare type ReplacerFunc<V extends React.ReactNode, S> = (value: V, state?: S) => React.ReactNode;
export declare type StateUpdateFunc<S> = (input: React.ReactNode, state?: S) => S;
declare type ReplacerSharedProps<StateType = any> = {
replace: ReplacerFunc<any, StateType>;
updateState?: StateUpdateFunc<StateType>;
initialState?: StateType;
};
export declare type ReplacerProps<StateType = any> = ReplacerSharedProps<StateType> & ({
match: MatchFunc<StateType>;
} | {
matchElement: React.ComponentType | string;
} | {
matchLiteral: MatchFunc<StateType>;
});
export declare function replaceInTree<S = any>(node: React.ReactNode, args: ReplacerProps<S>): React.ReactNode;
export {};