React Native <ListItem onPress = {...}> - 为什么这个函数在执行?

old*_*cho 14 javascript react-native native-base react-native-router-flux

我刚刚开始围绕React Native,并使用Redux来管理我的UI组件stateNativeBase库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 -视图,显示所选的细节guestView1.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)}}

  • @ oldo.nicho我们在那里传递一个回调.如果你在不执行函数的情况下像onPress = {goToView}那样做,那就行了.但是当你需要传递参数时,你应该执行一个函数来执行一个带有所需参数的函数.我很抱歉,如果解释是坏的,这是深夜这里)) (4认同)
  • 传奇!我一直在绞尽脑汁……为什么我打字的方式不起作用? (2认同)
  • 只是为了增加一些清晰度,我要解释的方式是您说的是在渲染ListItem时,调用函数goToView2并将返回值传递给backListItem,使其在按下时调用。因此,即使没有被按下,它仍然被自己调用。 (2认同)