old*_*cho 14 javascript react-native native-base react-native-router-flux
我刚刚开始围绕React Native,并使用Redux来管理我的UI组件state的NativeBase库和react-native-router-flux正在处理视图之间的导航.
目前我正在构建一个从一组guest对象创建的基本列表.该列表存储在Redux存储中,我可以访问它并相应地显示.列表中的每个项目都与数组guest内的项目相关联guests.我想使列表中的每个项目都可触摸,然后将相关guest对象作为属性传递给下一个视图以显示详细信息.
为此,我使用的onPress函数是与ListItem组件关联的标准函数(请参阅此处的 NativeBase文档).我还遵循了文档,react-native-router-flux并在组件本身之外定义了导航操作,以便ListItem在按下时调用它,而不是每次渲染时调用.
我的问题是,我发现onPress={goToView2(guest)}每次ListItem渲染时都会调用函数,而不是在ListItem按下时调用函数.
我确信它一定是简单的,我省略了.有什么建议?
View1.js - 显示以下初始列表的视图guests:
import React, { Component } from 'react';
import { Container, Header, Title, Content, Footer, FooterTab, Button, Icon,
Text, List, ListItem } from 'native-base';
import { connect } from 'react-redux';
import { Actions as NavigationActions } from 'react-native-router-flux';
class View1 extends Component {
render() {
// This is the function that runs every time a ListItem component is rendered.
// Why is this not only running on onPress?
const goToView2 = (guest) => {
NavigationActions.view2(guest);
console.log('Navigation router run...');
};
return (
<Container>
<Header>
<Title>Header</Title>
</Header>
<Content>
<List
dataArray={this.props.guests}
renderRow={(guest) =>
<ListItem button onPress={goToView2(guest)}>
<Text>{guest.name}</Text>
<Text note>{guest.email}</Text>
</ListItem>
}
>
</List>
</Content>
<Footer>
<FooterTab>
<Button transparent>
<Icon name='ios-call' />
</Button>
</FooterTab>
</Footer>
</Container>
);
}
}
const mapStateToProps = state => {
console.log('mapStateToProps state', state);
return { guests: state.guests };
};
export default connect(mapStateToProps)(View1);
Run Code Online (Sandbox Code Playgroud)
View2.js -视图,显示所选的细节guest从View1.js:
import React, { Component } from 'react';
import { Container, Header, Title, Content, Footer, FooterTab, Button, Icon,
Text, List, ListItem } from 'native-base';
class View2 extends Component {
render() {
console.log('View2 props: ', this.props);
return (
<Container>
<Header>
<Title>Header</Title>
</Header>
<Content>
<Content>
<List>
<ListItem>
<Text>{this.props.name}</Text>
</ListItem>
</List>
</Content>
</Content>
<Footer>
<FooterTab>
<Button transparent>
<Icon name='ios-call' />
</Button>
</FooterTab>
</Footer>
</Container>
);
}
}
export default View2;
Run Code Online (Sandbox Code Playgroud)
phi*_*psh 38
试着做 <ListItem button onPress={() => {goToView2(guest)}}
| 归档时间: |
|
| 查看次数: |
22066 次 |
| 最近记录: |