Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamic loop creation DOM,passing a callback function as a component property,cannot be obtained in the component #8636

Closed
no5no6 opened this issue Jul 7, 2016 · 3 comments
Labels
Ran Commands One of our bots successfully processed a command. Resolution: Locked This issue was locked by the bot.

Comments

@no5no6
Copy link

no5no6 commented Jul 7, 2016

I packaged a component, import the component,dynamic loop creation DOM , Passing a callback function as the component property,In packaged component, the property is undefined,I use the method is wrong?

in AnimateImage.js
This line of code is wrong

// print undefined, If don't create DOM dynamically, this property can be obtained.
console.log(onPress, 'onPress');  

Game.js

import React, { Component } from 'react';
import {
  AppRegistry,
  StyleSheet,
  TouchableWithoutFeedback,
  Text,
  View

} from 'react-native';

import AnimateImage from '../control/AnimateImage';
import _ from 'lodash';

export default class Game extends Component {
  constructor(props){
    super(props);
    let {imageArray} = this.props;
    this.state = {
      rows: [],
      imageArray: imageArray || [],
    };
  };

  componentDidMount(){
    this.createPokers();
  };

  onPress = () => {
    console.log('sucess');
  }

  //imageArray = [1,2,3,4]
  createPokers = () => {
    let self = this;
    let rows = imageArray.reduce(function(memo, value){
      memo.push(
          <AnimateImage
            key={'backImage'+ value}
            style={{left: 0 - value * 120}}
            animate="bounceInLeft"
            imageHeight={220}
            imageWidth={150}>
            onPress={self.onPress}
          </AnimateImage>
      );
      return memo;
    }, []);
    this.setState({rows: rows});
  };


  render() {
    return (
      <View>
        <View ref="viewPoker" style={[styles.viewRow, styles.AnimateImage]}>
          {this.state.rows}
        </View>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  viewRow: {
    flexDirection: 'row',
    position: 'absolute',
  },
  AnimateImage: {
    left: 150,
    top:300,
  }
});

AppRegistry.registerComponent('Game', () => Game);

AnimateImage.js

import React, { Component } from 'react';
import {
  AppRegistry,
  StyleSheet,
  Image,
  TouchableWithoutFeedback,
  Text,
  View
} from 'react-native';

import * as Animatable from 'react-native-animatable';

export default class AnimateImage extends Component {
  constructor(props){
    super(props);
    let {animate, text, imageHeight, imageWidth, style, onPress} = this.props;
    console.log(onPress, 'onPress');  // print undefined
    this.state = {
      animate: animate || '',
      imageHeight: imageHeight || 300,
      imageWidth: imageWidth || 200,
      style: style || {},
      callback: onPress || {}
    };
  };

  buttonPress = () => {
    this.refs.view.fadeOutUpBig(1500).then((endState) => {
      this.state.callback();  
    });
  };

  render() {
    return (
      <TouchableWithoutFeedback onPress={this.buttonPress}>
        <Animatable.View animation={this.state.animate} ref="view">
          <Image
            style={[this.state.style, {height: parseInt(this.state.imageHeight), width:parseInt(this.state.imageWidth)}]}
            source={require('./../../src/images/poker-back.png')}
          />
        </Animatable.View>
      </TouchableWithoutFeedback>
    );
  }
}



AppRegistry.registerComponent('AnimateImage', () => AnimateImage);

@ide
Copy link
Contributor

ide commented Jul 7, 2016

@facebook-github-bot stack-overflow

@ghost
Copy link

ghost commented Jul 7, 2016

Hey @LittleChild and thanks for posting this! @ide tells me this issue looks like a question that would be best asked on StackOverflow. StackOverflow is amazing for Q&A: it has a reputation system, voting, the ability to mark a question as answered. Because of the reputation system it is likely the community will see and answer your question there. This also helps us use the GitHub bug tracker for bugs only. Will close this as this is really a question that should be asked on SO.

@ghost ghost added the For Stack Overflow label Jul 7, 2016
@ghost ghost closed this as completed Jul 7, 2016
@ghost ghost added the Ran Commands One of our bots successfully processed a command. label Jul 7, 2016
@no5no6
Copy link
Author

no5no6 commented Jul 8, 2016

I see. Thank you.

@facebook facebook locked as resolved and limited conversation to collaborators May 24, 2018
@react-native-bot react-native-bot added the Resolution: Locked This issue was locked by the bot. label Jul 19, 2018
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Ran Commands One of our bots successfully processed a command. Resolution: Locked This issue was locked by the bot.
Projects
None yet
Development

No branches or pull requests

3 participants