score:1

Accepted answer

it seems like you need to sync the jobid state between the titlecycle component and the linechart component, so that both has access to this variable, and can render stuff in sync (title changes and linechart changes also).

in that case, you need jobid as a somewhat global variable. i think of this solution.

this seems to be the current hierachy

app
|-- linechart
|-- title
    |-- titlecycle

therefore, put the jobid in the common ancestor and prop drills it down. in this case, instead of generating jobid and index from titlecycle and having to lift the state back up and around into linechart, you shall cycle the indexes right inside app.js. something like this:

// app.js
function app() {
    const [alldata, setalldata] = usestate([]);
    const [index, setindex] = usestate(0);

    // fetch all data, all jobs
    useeffect(() => {
        function fetchdata() {
            let body = {
                from: "bpz99ram7",
                select: [3, 6, 40],
                where: "{40.ct. 'in progress'}",
                sortby: [{ fieldid: 6, order: "asc" }],
                groupby: [{ fieldid: 40, grouping: "equal-values" }],
                options: { skip: 0, top: 0, comparewithapplocaltime: false },
            };

            fetch("https://api.quickbase.com/v1/records/query", {
                method: "post",
                headers: headers,
                body: json.stringify(body),
            })
                .then((response) => response.json())
                .then(({ data }) => setalldata(data));
        }
        fetchdata();
    }, []);

    // cycle through the jobids and indexes
    useeffect(() => {
        const timerid = setinterval(
            () => setindex((i) => (i + 1) % alldata.length),
            5000 // 5 seconds.
        );
        return () => clearinterval(timerid);
    }, [alldata]);

    // calculate info based on index
    const jobid = alldata[index]?.["3"]?.value || "290"; // default "290"
    const title = alldata[index]?.["6"]?.value || "default title"; 

    // renders the components with the necessary data
    return (
        <div>
            <title title={title} />
            <linechart1 jobid={jobid} />
            <linechart2 jobid={jobid} />
        </div>
    );
}

// linechart.js
function totallinechart(props) {
    // got the jobid from props
    const { jobid } = props;

    // now make the request with this jobid
}

Related Query

More Query from same tag