streamlit-component-lib-react-hooks
Version:
An npm package that provides support code for creating [Streamlit Components](https://docs.streamlit.io/en/stable/streamlit_components.html) with React and React Hooks.
24 lines (23 loc) • 926 B
JavaScript
import { useState, useEffect } from "react";
import { Streamlit } from "streamlit-component-lib";
/**
* Returns `RenderData` received from Streamlit after the first render event received.
*/
export const useNullableRenderData = () => {
const [renderData, setRenderData] = useState();
useEffect(() => {
const onRenderEvent = (event) => {
const renderEvent = event;
setRenderData(renderEvent.detail);
};
// Set up event listeners, and signal to Streamlit that we're ready.
// We won't render the component until we receive the first RENDER_EVENT.
Streamlit.events.addEventListener(Streamlit.RENDER_EVENT, onRenderEvent);
Streamlit.setComponentReady();
const cleanup = () => {
Streamlit.events.removeEventListener(Streamlit.RENDER_EVENT, onRenderEvent);
};
return cleanup;
}, []);
return renderData;
};