[英]React Reduct - Select Onchange get method request
I have the following situation, i provide the a codesandbox demo to demonstrate my existing problem.我有以下情况,我提供了一个codeandbox 演示来演示我现有的问题。
A bit of explanation about and how to reproduce the case关于以及如何重现案例的一些解释
I am here in the component League.js
, any league in the list has a link我在组件
League.js
,列表中的任何联赛都有链接
<a href={`#${item.league_id}`} onClick={(e) => onClick(e, item.league_id)}>
const onClick = (evt, id) => {
evt.preventDefault();
getDetail(id)
};
The onclick function populates in the Details.js
component the select options
with the names of the teams. onclick 函数在
Details.js
组件中填充带有团队名称的select options
。 You can see below.你可以在下面看到。
if (teamsDetail.length) {
details = (
<select value={selectedOption} onChange={selectTeamStat}>
{teamsDetail && teamsDetail.length > 0
? teamsDetail.map(item => (
<option key={`${item.team_id}`} value={item.team_id}>
{item.name}
</option>
))
: null}
</select>
);
}
This is now the problem i have in Detail.js
component, when i select the team name in my select i want send a get request getStats
where i have to set two parameters ( team_id and league_id
)这现在是我在
Detail.js
组件中遇到的问题,当我在选择中选择球队名称时,我想发送一个获取请求getStats
,我必须在其中设置两个参数( team_id and league_id
)
Here the relevant code这里是相关代码
const [selectedOption, setSelectedOption] = useState("");
const selectTeamStat = evt => {
const { value } = evt.target;
setSelectedOption(value);
getStats(357, value);
};
At the moment i can only pass the team_id
which is the select value selectedOption
目前我只能传递
team_id
这是选择值selectedOption
How can i also pass the league_id
parameter?我怎样才能传递
league_id
参数?
I have hardcoded now putting 357
but i need to use the parameter league_id
.我现在已经硬编码了
357
但我需要使用参数league_id
。 How can i pass it from League.js
Component?我如何从
League.js
组件传递它?
On the top of my head, here's two solutions you may want to try:在我的脑海中,您可能想尝试以下两种解决方案:
Set each of you select's options as a tuple:将您选择的每个选项设置为一个元组:
value={[item.league_id, item.team_id]}
or set each of you select's options' team_id
and league_id
as data-attributes或将您选择的每个选项的
team_id
和league_id
为数据属性
I have figured it out how to pass the leagueId
state我已经弄清楚如何通过
leagueId
状态
These the steps i have done这些我已经完成的步骤
In the reducers/index.js
i have created an initial state在
reducers/index.js
我创建了一个初始状态
RECEIVE_LEAGUE
league:[],
case RECEIVE_LEAGUE:
return {...state, leagueId: action.json};
In the actions/index.js
在
actions/index.js
export const receivedLeague = json => ({
type: RECEIVE_LEAGUE,
json: json
});
I have added a dispatch in the getTeamsDetailById(id)
我在
getTeamsDetailById(id)
添加了一个调度
dispatch(receivedLeague(id));
In the Details.js
component在
Details.js
组件中
I have added the state leagueId
on top我在顶部添加了状态
leagueId
and edited my selectTeamStat function并编辑了我的 selectTeamStat 函数
const [selectedOption, setSelectedOption] = useState('');
const selectTeamStat = (evt) => {
const { value } = evt.target;
setSelectedOption(value)
getStats(leagueId, value);
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.