简体   繁体   English

我可以在这里设置状态吗? React-Native

[英]Can i set the state here? React-Native

In the getMatches function, I want to set the state, with the matches associated with the leagueID. 在getMatches函数中,我想设置状态以及与LeagueID相关的匹配项。 Where I am running into problems is how to setState in the get Matches function.The fetch response gives me exactly what I want back. 我遇到问题的地方是如何在get Matches函数中设置setState.fetch响应给出了我想要返回的确切信息。 Its an array with matches associated with the LeagueID. 它是一个与LeagueID关联的匹配项的数组。 That setState will run twice because I am using 2 leagueIds. 该setState将运行两次,因为我使用了2个LeagueIds。 Any help or advice would be greatly appreciated. 任何帮助或建议,将不胜感激。

 export default class MatchView extends Component { state = { matches: null, leagueName: null } componentWillMount = () => { this.getLeagueNames(); }//end componentwwillMOunt // componentDidMount = () => { // console.log("league_array", this.state.leagueName) // } getLeagueNames = () => { let leagueArray = []; fetch(`https://apifootball.com/api/?action=get_leagues&APIkey=42f53c25607596901bc6726d6d83c3ebf7376068ff89181d25a1bba477149480`) .then(res => res.json()) .then(leagues => { leagues.map(function(league, id){ leagueArray = [...leagueArray, league] })//end .map this.setState({ leagueName: leagueArray }); }) }// end getLeagueNames getMatches = (leagueID) => { let LeagGamesArray = []; console.log('working') fetch(`https://apifootball.com/api/?action=get_events&from=2016-10-30&to=2016-11-01&league_id=${leagueID}&APIkey=42f53c25607596901bc6726d6d83c3ebf7376068ff89181d25a1bba477149480`) .then(res => res.json()) .then(fixtures => { LeagGamesArray = [...LeagGamesArray, fixtures] ** this.setState({ matches: LeagGamesArray }) ** Setting the state here dosent work, just keeps running the code over and over again.**strong text** }) } //console.log(this.state.matches) //console.log(this.state.matches) //console.log(this.state.matches) renderItem = () => { if(this.state.leagueName != null){ return this.state.leagueName.map(function(league){ let leagueID = league.league_id let fix = this.getMatches(leagueID) //console.log(fix) return ( <LeagueName key={league.country_id} league={league}/> ) }.bind(this))//end maps..We .bind(this) b/c we want to change the meaning of this. We first use it in the .map function, but we also want to use this to call the getMacthes function. } else { return null; } }//end renderItem render(){ return ( <ScrollView> {this.renderItem()} </ScrollView> ) } } 

strong text 强文本

Your renderItem method calls getMatches(leagueID) , which in turn fires-off your fetch which results in setState and a subsequent render ... and repeat. 您的renderItem方法调用getMatches(leagueID) ,这反过来触发您的fetch ,从而导致setState和随后的render ...并重复。

You should call getMatches from elsewhere, like componentWillMount . 您应该从其他地方调用getMatches ,例如componentWillMount Or by some user interaction (or even timer) that you use to generate a refresh. 或者通过一些用户交互(甚至是计时器)来生成刷新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法在 js react-native 中设置 state - Can't set state in js react-native 如何映射对象数组,删除一个对象并将我的状态设置为 react-native 中的新数组? - How can I map through an array of objects, remove one and set my state as the new array in react-native? React-native映射:尝试将initialRegion的纬度和经度设置为状态数据时出错 - React-native maps: Error when I try to set latitude and longitude of initialRegion to state data 根据react-native中传递的参数设置动态状态名称 - Set dynamic state name based on the delivered parameter in react-native 我无法在我的 react-native 应用程序中设置初始状态 - Im not able to set an initial state in my react-native application State 仅在第二次渲染时设置(react-native) - State only set on second render (react-native) react-native设置全局状态并使用redux进行回调 - react-native set global state and call back using redux React-Native:无法从表单值设置状态 - React-Native: Cannot set state from form values React-Native-我不能将状态字符串用作新的列表元素 - React-Native - I can not use State string as new list element 设置状态[react-native]后无法编辑inputText - Can't edit inputText after setting state [react-native]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM