在Press上反应Native Picker

时间:2018-08-02 09:16:29

标签: react-native

我是本机反应的新手。在我的代码中,我使用了本机选择器。当我点击选择器时,我希望pickerActivity()被触发,但它甚至在我点击之前就被触发(在点击之前被触发三次)。下面是我的代码

server {
    listen 80;

    server_name ~^(?<folder>[^.]*).(?<user>[^.]*).dev.example.com;

    charset utf-8;
    index index.php index.html index.htm;



    root /var/www/projects/dev/$user/$folder/htdocs;
    access_log /var/www/projects/dev/$user/$folder/access.log;
    error_log  /var/www/projects/dev/commons/logs/htdocs/error.log;

    location ~* .(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|rss|atom|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf|js|css)$ {
        access_log        off;
        log_not_found     off;
        expires           30d;
        add_header Pragma "public";
    }

    location / {
        try_files $uri $uri/ /index.php?$args;
        auth_basic "Restricted Content";
        auth_basic_user_file /etc/nginx/.htpasswd;
    }

    location ~ \.php$ {

            try_files $uri =404;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass unix:/run/php/php7.0-fpm.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            fastcgi_param APPLICATION_ENV $user;
            include fastcgi_params;
   }


    location ~ /\.ht {
        deny all;
    }
}

4 个答案:

答案 0 :(得分:2)

注意:我看不到任何onPress道具可供选择。相反,您可以尝试onValueChange。这是doc

这是将onValueChange事件作为目标的沙箱link,这里是我们如何绑定onPress事件的方法。

onPress={this.pickerActivity.bind(this)



您可以使用箭头功能,如

onPress={(e)=>this.pickerActivity(e)} < br />
使用箭头功能的优点是您不必担心this上下文。 查看有关事件here的更多信息。

答案 1 :(得分:1)

您不需要onPress()函数。您可以使用onValueChange完成所有这些操作。

试试看...

pickerActivity = (val) =>{
    this.setState({changedVal: val})
    alert("PICKER PRESSED")
  }

render(){
return(
  <Picker
    selectedValue={this.state.changedVal}
    style={{ height: 50, width: 100 }}
    onValueChange={(itemValue, itemIndex) => this.pickerActivity(itemValue)}>
    <Picker.Item label="Java" value="java" />
    <Picker.Item label="JavaScript" value="js" />
  </Picker>
  )
}

答案 2 :(得分:1)

constructor(props) {
    super(props);
    this.state = {
        selected: "key1"
    };
}
onValueChange(value: string) {
    this.setState({
        selected: value
    });
}

render() {
    return (
        <Container>
            <Content>
                <Form>
                    <Picker
                        note
                           mode="dropdown"
                        style={{ width: 120 }}
                        selectedValue={this.state.selected}
                        onValueChange={this.onValueChange.bind(this)}
                    >
                        <Picker.Item label="Wallet" value="key0" />
                        <Picker.Item label="ATM Card" value="key1" />
                    <Picker.Item label="Debit Card" value="key2" />
                    <Picker.Item label="Credit Card" value="key3" />
                    <Picker.Item label="Net Banking" value="key4" />
                </Picker>
     </Form>
    </Content>
</Container>

答案 3 :(得分:0)

     Call the function inside the onValueChange passing the value as a 
     parameter  
     <RNPickerSelect
              placeholder={{ label: `Estado`, value:null , }}
              style={pickerStyle}
              onValueChange={(value) => [this.setState({ estado: value }), this.cidades(value)]}
              items={
                estados.map((item, index) => {
                  return { label: `${item.state}`, value: `${item.code}` }
                })
             }
           />