我将一个对象作为道具传递给了我的组件。我可以访问它,并将它作为一个对象返回,但是当我尝试访问该对象之外的某个项目(我确定它存在)时,它说未定义。
运行此命令时:
import React, { Component } from 'react';
import { Text } from 'react-native';
import { CardSection } from './common';
class ListItem extends Component {
render() {
const { titleStyle } = styles;
console.log(this.props.library);
console.log(this.props.library.title);
return (
<CardSection>
<Text style={titleStyle}>{this.props.library.title}</Text>
</CardSection>
);
}
}
我明白了:
{…}
index: 0
item: Object { id: 0, title: "Webpack", description: "Webpack is a module bundler. It packs CommonJs/AMD modules i. e. for the browser. Allows to split your codebase into multiple bundles, which can be loaded on demand." }
undefined
最后console.log()
的结果在底部。
这是我传递库对象的组件:
import React, { Component } from 'react';
import { FlatList } from 'react-native';
import { connect } from 'react-redux';
import ListItem from './ListItem';
class LibraryList extends Component {
renderItem(library) {
return <ListItem library={library} />;
}
render() {
return (
<FlatList
data={this.props.libraries}
renderItem={this.renderItem}
keyExtractor={library => library.id.toString()}
/>
);
}
}
const mapStateToProps = (state) => ({ libraries: state.libraries });
export default connect(mapStateToProps)(LibraryList);
在此处查看完整代码:https://github.com/ceerees12/tech_stack
答案 0 :(得分:0)
您没有在组件中传递title
,如果您在title
组件中传递了道具ListItem
,那么您将能够访问this.props.title
,并且如果title
处于library
属性中,则应该this.props.library.title
。
答案 1 :(得分:0)
尝试将import random
import pandas as pd
import numpy as np
from matplotlib import pyplot as plt
p = [.1,.2,.3,.4,.5,.6,.7, .8,.9]
p = [random.choice(p) for _ in range(180)]
r = np.linspace(.001, .999, num=180)
df = pd.DataFrame({'pct':p, 'ratio':r})
df1 = df.groupby('pct')['ratio'].mean()
df1.plot()
plt.show()
plt.close()
更改为renderItem={this.renderItem}