react#Props TypeScript Examples

The following examples show how to use react#Props. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: useSSE.tsx    From useSSE with MIT License 6 votes vote down vote up
createBroswerContext = (
  variableName: string = "_initialDataContext"
) => {
  const initial = window && window[variableName] ? window[variableName] : {};
  let internalContextValue: IInternalContext = {
    current: 0,
    resolved: true,
    requests: [],
  };
  function BroswerDataContext<T>(props: Props<T>) {
    return (
      <InternalContext.Provider value={internalContextValue}>
        <DataContext.Provider value={initial}>
          {props.children}
        </DataContext.Provider>
      </InternalContext.Provider>
    );
  }

  return BroswerDataContext;
}
Example #2
Source File: useSSE.tsx    From useSSE with MIT License 5 votes vote down vote up
createServerContext = () => {
  let ctx: IDataContext = {};
  let internalContextValue: IInternalContext = {
    current: 0,
    resolved: false,
    requests: [],
  };
  function ServerDataContext<T>(props: Props<T>) {
    return (
      <InternalContext.Provider value={internalContextValue}>
        <DataContext.Provider value={ctx}>
          {props.children}
        </DataContext.Provider>
      </InternalContext.Provider>
    );
  }
  const resolveData = async (timeout?: number) => {
    const effects = internalContextValue.requests.map((item) => item.promise);

    if (timeout) {
      const timeOutPr = wait(timeout);

      await Promise.all(
        internalContextValue.requests.map((effect, index) => {
          return Promise.race([effect.promise, timeOutPr]).catch(() => {
            return effect.cancel();
          });
        })
      );
    } else {
      await Promise.all(effects);
    }

    internalContextValue.resolved = true;
    internalContextValue.current = 0;
    return {
      data: ctx,
      toJSON: function () {
        return this.data;
      },
      toHtml: function (variableName: string = "_initialDataContext") {
        return `<script>window.${variableName} = ${JSON.stringify(
          this
        )};</script>`;
      },
    };
  };
  return {
    ServerDataContext,
    resolveData,
  };
}