简体   繁体   English

React Reduct - 选择 Onchange get 方法请求

[英]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_idleague_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 statereducers/index.js我创建了一个初始状态

RECEIVE_LEAGUE
  league:[],

case RECEIVE_LEAGUE:
  return {...state, leagueId: action.json};

In the actions/index.jsactions/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 componentDetails.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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM