I’m studying the custom widgets with the Minimal example. I can make the widget showing JSON of selected record as the instructions. Now I want to extract values of single columns, e.g. Name to display in the HTML. Please help me find a way to do.
Thanks.
My custom widget is now working great, yet the exported documents consist of many pages with different formats, therefore I have to look for a better approach utilizing components of ReactJS.
Currently, I can’t fetch data from grist-plugin-api as the below code.
Hi @huyluong82! The call to grist.onRecord is setting a callback to a function that will accept records, it doesn’t itself return a record. I don’t know React, but I was able to tweak that code to work as follows:
let gristSetData; // this will be a method to ingest data from Grist to React
grist.ready();
grist.onRecord((record) => {
if (gristSetData) { gristSetData(record); }
});
function App() {
const [data, setData] = React.useState({});
gristSetData = setData;
return (
<p>Hello {data.Name}</p>
);
}
ReactDOM.render(<App />, document.getElementById("root"));
Not sure if this is sane React code, apologies if not. The main trick will be to have the onRecord callback provide data via whatever state management mechanism makes React most happy.
@paul-grist I think your code is ok. But React has some great tools to handle intializing states that makes code a little nicer.
Please allow me to share how I would do it in React:
Hi @paul-grist , I’m also new to React, thus there are many things to learn. I really appreciate your support. I’m considering move the database of my small business to Grist, and so far so good.
Hi @cpind , thank you for your help. It works beautifully.