Calling class method from inside conditionally rendered JSX

SKeney

Trying to call a class method from inside JSX that is stored inside a variable inside the class and conditionally rendered dependent on which step the user is on. I am pretty sure that the issue is with my attempt using this.

class CreateGameModal extends React.Component {
    constructor(props) {
        super(props)
        this.state = {
            showChooseGameType: true,
            showGameLocation: false,
            showGameInfo: false,
            showSuggestedInvites: false,
            showConfirmation: false
        };

        this.onGameCreate = this.onGameCreate.bind(this);
    }

    render() {
        const { hideModal } = this.props;
        const { showChooseGameType, showGameLocation, showGameInfo, showSuggestedInvites, showConfirmation } = this.state;

        return (
                <Modal style={styles.modal} isVisible={this.props.isVisible} onBackdropPress={hideModal}>
                    { showChooseGameType &&
                        this.chooseGameCard
                    }

                    { showGameLocation &&
                        this.chooseGameLocationCard
                    }

                    { showGameInfo &&
                        this.chooseGameInfoCard
                    }

                    { showSuggestedInvites &&
                        this.chooseSuggestedInvitesCard
                    }

                    { showConfirmation &&
                        this.showConfirmationCard
                    }
                </Modal>
        );
    }

    chooseGameCard = ( 
        <View style={styles.createCard}>
            <Text style={styles.titleText} numberOfLines={1} >Create Game</Text>
            <ScreenStageBar numberOfStages={4} currentStage={1}/>
            <Text style={styles.descText} numberOfLines={1}>What type of game would you like to create?</Text>
            <Button title="Next" onPress={() => {
                this.setState({ showChooseGameType: false })
                this.setState({ showGameLocation: true })
            }} />
        </View>
    );

    chooseGameLocationCard = (
        <View style={styles.createCard}>
            <Text style={styles.titleText} numberOfLines={1} >Choose Game Location!</Text>
            <ScreenStageBar numberOfStages={4} currentStage={2}/>
            <Button title="Next" onPress={() => {
                this.setState({ showGameLocation: false })
                this.setState({ showGameInfo: true })
            }} />
        </View>
    );

    chooseGameInfoCard = (
        <View style={styles.createCard}>
            <Text style={styles.titleText} numberOfLines={1}  >Enter Game Information</Text>
            <ScreenStageBar numberOfStages={4} currentStage={3}/>
            <Button title="Next" onPress={() => {
                this.setState({ showGameInfo: false })
                this.setState({ showSuggestedInvites: true })
            }} />
        </View>
    );

    showConfirmationCard = (
        <View style={styles.createCard}>
            <Text style={styles.titleText} numberOfLines={1} >Congratulations!</Text>
        </View>
    );

    chooseSuggestedInvitesCard = (
        <View style={styles.createCard}>
            <Text style={styles.text}>Suggested Invites</Text>
            <ScreenStageBar numberOfStages={4} currentStage={4}/>
            <Button title="Create Game" onPress={this.onGameCreate} />
        </View>
    );

    onGameCreate = () => {
        //TODO: Add game creation logic with back end
        console.log("Press!");
        this.setState({ showSuggestedInvites: false });
        this.setState({ showConfirmation: true });
    }
}

The "Press" console log is not being logged and thus is not rendering the last bit of content I want to show after hitting the create game button.

Praful

You can use Arrow function like this:

onGameCreate = () => {
    //TODO: Add game creation logic with back end
    console.log('Press!')
    this.setState({ showSuggestedInvites: false });
    this.setState({ showConfirmation: true });
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Calling a method from inside of another class

From Dev

calling static method from inside the class

From Dev

Calling a method from another method inside the same class using getattr

From Dev

Calling Attributes from Class inside of a Class Method, Code Error

From Dev

Calling a method from inside a thread to another class in python pyqt

From Dev

Calling a method inside Vue component from an outside class

From

Calling method from a Angular 2 class inside template

From Dev

Calling a abstract method from a callback inside a abstract class

From Dev

Calling a method from a class

From Java

Calling a method inside another method in same class

From Dev

Flutter: how to conditionally run method inside ChangeNotifier after calling constructor

From Dev

Calling a function inside JSX

From Dev

For HTML rendered with PHP, which method is faster for putting CSS to such HTML. Including incline CSS or calling a class from the receiving page?

From Java

Calling super method from within an anonymous inner class inside the overridden method

From Dev

Calling a synchronized method from a new thread created inside another synchronized method of the same class in Java

From Dev

Calling and passing to a method from class

From Javascript

Calling a class method from the constructor

From Dev

Calling method from State class

From Dev

Calling a function from a class method

From Dev

Calling method from a different class

From Dev

calling a method from tkinter class

From Dev

Calling class method inside string format

From Java

Calling a static method inside a class in jar file

From Dev

Calling Methods Inside Another Method In a Different Class

From Dev

__call__ when calling a method inside the class

From Dev

calling a method inside a class-Python

From Java

Calling private method inside private class inside Inner class

From Dev

ajax listener inside conditionally rendered element not working

From Java

Calling method of Another class from run() method

Related Related

  1. 1

    Calling a method from inside of another class

  2. 2

    calling static method from inside the class

  3. 3

    Calling a method from another method inside the same class using getattr

  4. 4

    Calling Attributes from Class inside of a Class Method, Code Error

  5. 5

    Calling a method from inside a thread to another class in python pyqt

  6. 6

    Calling a method inside Vue component from an outside class

  7. 7

    Calling method from a Angular 2 class inside template

  8. 8

    Calling a abstract method from a callback inside a abstract class

  9. 9

    Calling a method from a class

  10. 10

    Calling a method inside another method in same class

  11. 11

    Flutter: how to conditionally run method inside ChangeNotifier after calling constructor

  12. 12

    Calling a function inside JSX

  13. 13

    For HTML rendered with PHP, which method is faster for putting CSS to such HTML. Including incline CSS or calling a class from the receiving page?

  14. 14

    Calling super method from within an anonymous inner class inside the overridden method

  15. 15

    Calling a synchronized method from a new thread created inside another synchronized method of the same class in Java

  16. 16

    Calling and passing to a method from class

  17. 17

    Calling a class method from the constructor

  18. 18

    Calling method from State class

  19. 19

    Calling a function from a class method

  20. 20

    Calling method from a different class

  21. 21

    calling a method from tkinter class

  22. 22

    Calling class method inside string format

  23. 23

    Calling a static method inside a class in jar file

  24. 24

    Calling Methods Inside Another Method In a Different Class

  25. 25

    __call__ when calling a method inside the class

  26. 26

    calling a method inside a class-Python

  27. 27

    Calling private method inside private class inside Inner class

  28. 28

    ajax listener inside conditionally rendered element not working

  29. 29

    Calling method of Another class from run() method

HotTag

Archive