我如何将 api 响应存储在变量中并将其传递给 React 弹出式视频播放器

How can i store api response in variable and pass it to react popup video player

使用fetch调用API并打印在控制台上。

app.js

const onGridReady = (params) => {
    console.log("grid is ready");
    fetch("http://localhost:8000/get_all")
      .then((resp) => resp.json())
      .then((resp) => {
        console.log(resp.results);
        params.api.applyTransaction({ add: resp.results });
      });
  };

使用 fetch 调用 api 并将其打印在控制台上。

api 响应:

Array(80)
0:
Camera_Number: "Camera_1"
Company_Name: "Fraction Analytics Limited"
Floor Number: "Ground_Floor"
Group_Name: "Group_1"
Video_Name: "http://localhost:4000/video/0"
[[Prototype]]: Object

1:
Camera_Number: "Camera_2"
Company_Name: "Fraction Analytics Limited"
Floor Number: "Ground_Floor"
Group_Name: "Group_1"
Video_Name: "http://localhost:4000/video/1"

我从 API 得到了这个回复。现在如何在反应 js 变量中存储 API 响应。存储数据后如何通过 Video_Name:"http://localhost:4000/video/*" 来响应播放器源

将此响应分配给 React table :

 const columnDefs = [
    { headerName: "Name", field: "Company_Name", filter: "agSetColumnFilter" },
    { headerName: "Floor", field: "Floor Number" },
    { headerName: "Group", field: "Group_Name" },
    { headerName: "Camera", field: "Camera_Number" },
    { headerName: "Videos", field: "Video_Name" },
    {
      headerName: "Actions",
      field: "Video_Name",
      cellRendererFramework: (params) => (
        <div>
          <Button
            variant="contained"
            size="medium"
            color="primary"
            onClick={() => actionButton(params)}
          >
            Play
          </Button>
        </div>
      ),
    },
  ];



<DialogContent>
  <iframe
    width="420"
    height="315"
    title="videos"
    src={("http://localhost:4000/video/0", "http://localhost:4000/video/1")}
  />
</DialogContent>;

更多代码参考

CLICK HERE

您应该使用 useState 挂钩来存储来自 API

的响应
const [response, setResponse] = useState([]);

const onGridReady = (params) => {
    console.log("grid is ready");
    fetch("http://localhost:8000/get_all")
      .then((resp) => resp.json())
      .then((resp) => {
        setResponse(resp.results);
        params.api.applyTransaction({ add: resp.results });
      });
  };

然后最终使用iframe中的数组,并为每个link输出一个iframe。

<DialogContent>
  {response.map(({Video_Name})=> 
  <iframe
    width="420"
    height="315"
    title="videos"
    src={Video_Name}
    />
  )}
</DialogContent>;

无需使用按钮的点击处理程序。你不能用它来获取行数据。

colId 添加到操作的列定义中。然后处理网格的onCellClicked动作。您可以使用 params.node.data.

获取行数据
const [videoName, setVideoName] = useState("");

function onCellClicked(params) {
    // maps to colId: "action" in columnDefs,
    if (params.column.colId === "action") {
      // set videoName for the row
      setVideoName(params.node.data.Video_Name);
      setOpen(true);
    }
}

// grid config
<AgGridReact
  ...
  rowData={response}
  onCellClicked={onCellClicked}>
</AgGridReact>

// access videoName in dialog content
<DialogContent>
 {/* <iframe width="420" height="315" title="videos" src={id} /> */}
 <div>{videoName}</div>
</DialogContent>

Codesand box Link