tableflip / react-native-toaster

Simple top-pop toast feedback messages for React Native, also Redux compatible
ISC License
85 stars 29 forks source link

how to show up the toast using the onPress ? #8

Closed PManager1 closed 6 years ago

PManager1 commented 7 years ago
_onPressBu(){
console.log('30- clicing the onPressButton'); 
this.setState( message: 'Hooray!' );

}

ghost commented 6 years ago

Provide more information please.
Also, usually prefer StackOverflow if you have question related to usage rather than bug/issues.

jqn commented 6 years ago

I don't think this has a option to show the toast if a button is pressed, judging by the source code:

  componentWillMount () {
    this.showToast()
  }

  componentWillReceiveProps (nextProps) {
    if (this.props.id !== nextProps.id) {
      this.showToast()
    }
  }

  showToast () {
    const animatedValue = new Animated.Value(0)

    this.setState({ animatedValue })

    Animated
      .timing(animatedValue, { toValue: 1, duration: 350 })
      .start()

    const { duration, onShow } = this.props
    const timeoutId = setTimeout(() => this.hideToast(), duration + 350)

    this.setState({ timeoutId }, onShow)
  }

It seems like it is set to show on component mount. This looks great but there is no npm installation info or clear instructions on usage.

alanshaw commented 6 years ago

If you want a toast message to show when you press a button you use your click handler to set some state that'll pass a new message prop to the component.

Something like this:

import React, { Component } from 'react'
import { View, Text } from 'react-native'
import Toaster from 'react-native-toaster'

class App extends Component {
  state = { message: null }

  onButtonPress = () => this.setState({ message: { text: 'Hello World!' } })

  render () {
    return (
      <View>
        <View onPress={this.onButtonPress}>
          <Text>Make toast</Text>
        </View>
        <Toaster message={this.state.message} />
      </View>
    )
  }
}