我有一个需求,我需要从数组中呈现一个表,并将其作为道具传递给组件。表中的每一行都包含下拉列表,从下拉列表中选择任意值时,我希望根据所选值调用外部API。API还需要td中的DropDown值,哪个key1值发生了变化?
我正面临一个问题,我无法将选定的操作和API值放入key1调用函数中。另外,如果我从dropdown中选择一个值,所有的dropdown值都会同时更改(我可以理解为什么会这样)。如何避免这种情况?或者,这是无法避免的,有没有更好的方法来以其他方式呈现相同的东西,以允许所需的功能?
export class RendererClass extends React.Component {
constructor(props) {
super(props);
this.callAPI = this.callAPI.bind(this);
this.state = {
container: "",
action: ""
};
}
callAPI() {
if (this.state.action === "a") {
// Fetch API call here
// requires container as well as action state which shoould be set to key1 from the same row whenever value is changed for dropdown
...
else {
// Alternate Fetch API call here
// requires container state which shoould be set to key1 from the same row whenever value is changed for dropdown
...
}
}
render() {
return (
<table>
<tr>
<th> Subject </th>
<th> Source </th>
<th> Actions </th>
</tr>
{this.props.alerts.map(alert => (
<tr key={alert.id.toString()}>
<td>{alert.key1}</td>
<td>{alert.key2}</td>
<td>
<select onChange={this.callAPI} value={this.state.action} name="actions" id="actions">
<option hidden disabled selected value>Select an action</option>
<option value="a">Action A</option>
<option value="b">Action B</option>
</select>
</td>
</tr>
))}
</table>
);
}
}另外,使用React Hooks实现此功能是否有帮助?我知道Hoooks的基础知识,但不知道Hooks是否会帮助实现所需的任务?
发布于 2021-04-17 08:44:31
我认为useState和useEffect的结合实际上会对此有所帮助。不确定您还需要做什么来过滤表或任何东西,但下面这样的代码应该会有所帮助:
import React, { useEffect, useState } from "react";
// I have a requirement where i render a table from
// array passed as props to the component. Table contain s
// dropdown in each row and on selecting any value from dropdown,
// I want to call external APIs based on the selected value.
// The API also requires key1 value from td for which dropdown
// value was changes?
// i am facing a problem where I can't get selected action
// and key1 value into the API call function. Plus, If i select a
// value from dropdown, all dropdown values are changed simultaneously
// (i can understand why it is happening). How to avoid this? Or may be,
// it can not be avoided, is there a better way to render same thing in
// other way which allows for the required functionality?
export default function ({ alerts }) {
// constructor(props) {
// super(props);
// this.callAPI = this.callAPI.bind(this);
// this.state = {
// container: "",
// action: ""
// };
// }
const [selectedAction, setSelectedAction] = useState({});
const [key1Value, setkey1Value] = useState();
useEffect(() => {
if (selectedAction === "a") {
// Fetch API call here should be able to access key1Value
// requires container as well as action state which shoould be set to key1 from the same row whenever value is changed for dropdown
//...
} else {
// Alternate Fetch API call here
// requires container state which shoould be set to key1 from the same row whenever value is changed for dropdown
//...
}
}, [selectedAction]);
return (
<table>
<tr>
<th> Subject </th>
<th> Source </th>
<th> Actions </th>
</tr>
{alerts.map((alert) => (
<tr key={alert.id.toString()}>
<td>{alert.key1}</td>
<td>{alert.key2}</td>
<td>
<select
onChange={(e) => {
setkey1Value(alert.key1);
setSelectedAction({...selectedAction, [alert.key1]: e.target.value});
}}
value={selectedAction[alert.key1]}
name="actions"
id="actions"
>
<option hidden disabled selected value>
Select an action
</option>
<option value="a">Action A</option>
<option value="b">Action B</option>
</select>
</td>
</tr>
))}
</table>
);
}沙盒链接,如果你想做调整和尝试:https://codesandbox.io/s/hungry-grass-hndhp?file=/src/App.js:0-1539
我认为如果你需要让输入跟踪不同的值,而不是一次全部改变,那么你可以使用一个对象来存储它们,其中键是alert.key1,值是当前选定的值。我觉得更多关于你真正想要做什么的信息可能会对你有所帮助。
https://stackoverflow.com/questions/67130529
复制相似问题