gpt4 book ai didi

ios - React-Native 与 Redux : API objects appear in my console. 日志但不是我的观点

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:30:40 25 4
gpt4 key购买 nike

我正在使用 reactreact-nativereact-native-router-flux 构建一个简单的运动应用react-redux,使用 redux 并尝试将热门新闻对象的 api 拉到我的 view。我需要的对象显示在我的控制台日志上,但无法让它显示在我的 View 上。

我得到的错误是:

Cannot read property 'cloneWithRowsAndSections' of undefined

TypeError: Cannot read property 'cloneWithRowsAndSections' of undefined at NewsList.componentWillMount

有什么我想念的吗?

新闻列表:

export default class NewsList extends Component {
constructor(props){
super(props);
const ds = new ListView.DataSource({
rowHasChanged: (row1, row2) => row1 != row2,
sectionHeaderHasChanged: (s1, s2) => s1 !== s2
});
this.state = {
dataSource: ds.cloneWithRowsAndSections(props.ListData)
}
}

componentWillReceiveProps(nextProps){
const dataSource = this.state.dataSource.cloneWithRowsAndSections(nextProps.ListData);
this.setState({dataSource});
}


componentWillMount(){
// console.log("whats This: ",this.props.ListData);
let data = this.props.ListData;
this.setState({
dataSource: this.ds.cloneWithRowsAndSections(data)
});
}

新闻 View :

render(){
console.log('TopNews', this.state.TopNews);
if(this.state.TopNews.length == 0){
return(
<View style={styles.container}>

<View style={styles.LiveStyle}>
<Live style={styles.LiveStyle} />
</View>

<View style={styles.container}>
<Text style={[styles.NotFollowTeamMsg, styles.centerTeamMsg]}>
Your not connected to the api
</Text>
</View>
</View>
)
} else {
return (
<View style={styles.container}>
<NewsList
ListData={this.state.TopNews}


/>
</View>
)
}


}

在构造函数中,我尝试将 const ds = ... 更改为 this.ds = ... 但这只会导致错误:

ds is not defined ReferenceError: ds is not defined

我尝试取出构造函数:

const ds = new ListView.DataSource({
rowHasChanged: (row1, row2) => row1 != row2,
sectionHeaderHasChanged: (s1, s2) => s1 !== s2
});

然后将它定位在 NewsList 类之外,然后我setState,像这样:

this.setState({
dataSource: ds.cloneWithRowsAndSections(data)
});

但这只会导致错误:

Cannot read property 'bind' of undefined TypeError: Cannot read property 'bind' of undefined

最佳答案

2个选项:

1.在你的构造函数中使const ds = ...,如this.ds = ...

2.从 constructor 函数中取出:

const ds = new ListView.DataSource({
rowHasChanged: (row1, row2) => row1 != row2,
sectionHeaderHasChanged: (s1, s2) => s1 !== s2
});

并将其定位在 NewsList 类之外。

现在,当您setState 时,请这样做:

this.setState({
dataSource: ds.cloneWithRowsAndSections(data)
});

关于ios - React-Native 与 Redux : API objects appear in my console. 日志但不是我的观点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38025388/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com