How do you make a modal pop-up without clicking a button in react native? I am currently using an alert to pop -up once the game state changes but I’d like to be able to style it so I want to change it to a modal view. I’m not sure how to implement it into my code 🙁 Could someone please teach me how?
const checkGameState = () => {
if (gameWon()) {
setGameState('won')
Alert.alert('You won!', [
{ text: 'play again', onPress: () => newGame() }
])
} else if (gameLost()) {
setGameState('lost')
Alert.alert('You Lost', [
{ text: 'try again', onPress: () => newGame() }
])
}
}
3
Answers
Use react-native-modalize for easy modals, this saved me alot of hours and pain.
I guess this is you were looking for
You can create GlobalModal at App.js, using ref = createRef()
when you want call it, you can call ref.current.open(…) everywhere
You could use react native’s modal component to get your custom styling. You would need to create a context to be able to easily access the alert parameters, and then create a hook to update those parameters (demo):
Context:
Alert Component:
Hook:
Usage: