javascript - 导航回来时 react native 重新运行功能

标签 javascript reactjs react-native

我正在使用 react 导航在屏幕之间导航,现在在我的“cases.js”中,我有一个 fetch 方法,当用户单击一个案例时,它会从 API 获取案例,它会加载另一个页面,其中包含单击的案例接受或否认案件本身。

无论哪种方式,如果用户接受案例,用户都将被重定向到“cases.js”页面,我不再需要该案例在“cases.js”页面列表上可用。

这是我的case.js 文件

export default class PendingCases extends Component {

  constructor(props) {

    super(props);

    this.state = { cases : [], user : '', refreshing : false }

  }

  componentDidMount = async () => {

    await this._getUser();

    await this._getCases();

  }

  _getUser = async () => {

    let user = await AsyncStorage.getItem('user');

    await this.setState({ user : JSON.parse(user) })

  }

  _getCases = async () => {

    try {

      await this.setState({ refreshing : true })

      let pendingCases = await fetch('http://192.168.1.101:1337/user/cases?status=pending&userType=patient&id=' + this.state.user.id);

      let response = await pendingCases.json();

      await this.setState({ cases : response.cases });

      await this.setState({ refreshing : false })

    } catch (err) {

      console.log(err);

    }

  }

  render() {

    let casesArray = this.state.cases.map((singleCase, index) => {
      return (
        <View key={ index }>
          <TouchableOpacity onPress={ () => this.props.navigation.navigate('SelectedCaseDetails') } style={ styles.caseButton }>
            <View>
              <View style={{ marginBottom: 10, marginTop: 5 }}>
                <LinearTextGradient locations={[0, 1]} start={{x: 0, y: 0}} end={{x: 1, y: 0}} colors={['#a4d294', '#3ac6f3']} style={{ fontWeight: 'bold' }}>
                  {singleCase.doctor.name}
                </LinearTextGradient>
                <Text style={{ position: 'absolute', right: 0, fontWeight: '600', color: 'black'}}> 00231 </Text>
              </View>
              <View style={{ marginBottom: 10 }}>
                <Text> {singleCase.additionalInformation} </Text>
              </View>
              <View style={{ marginBottom: 10, flex : 1, flexDirection: 'row'}}>
              <Image style={ styles.statusImage } source={require('../images/status.png')} />
                <Text style={{ marginRight : 30, color : 'rgba(0, 0, 0, .8)', flex : 8}}>
                 Status <Text style={{ color : 'rgb(240, 151, 166)', marginLeft : 60 }}> {singleCase.status} </Text> </Text>
                <Text style={{ position: 'absolute', right: 0, fontWeight: '300', color: 'grey'}}> { parsedDate } </Text>
              </View>
            </View>
          </TouchableOpacity>

        </View>
      )
    });

    return (
      <View>
        { casesArray }
      </View>
    )

  }

}

当我从一级深层页面重定向用户时,如何重新运行 this._getCases() 函数?

我以与导航到任何其他屏幕相同的方式重定向用户 this.props.navigation.navigate('CasesScreen')

最佳答案

如果你的组件已经安装,你可以使用react-navigation的 listeners检查组件是否获得焦点。

你需要

didFocus: the screen focused (if there was a transition, the transition completed)

componentDidMount () {
    this._getUser();
    this._onFocusListener = this.props.navigation.addListener('didFocus', (payload) => {
      this._getCases();
    });
}

并在组件卸载后将其删除。

componentWillUnmount () {
      this._onFocusListener.remove()
}

关于javascript - 导航回来时 react native 重新运行功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52172714/

相关文章:

javascript - 为什么表单收不到 AntD 中的选择数据?

javascript - react native 图标位于 View 下

javascript - Firebase Cloud Functions Http 请求 - 验证用户

javascript - 添加 SlideToggle 效果下拉菜单

javascript - 元素仅在 Safari 10 中消失(9 没问题),当我更改任何 css 时显示

javascript - 在 react 中触发 onClick 按键事件?

javascript - ES5中如何让构造函数指向 super 函数原型(prototype)

javascript - 报告的 DIV 高度在使用 Javascript/Jquery 刷新时发生变化

ios - react native 错误 ld : library not found for -lBVLinearGradient

reactjs - 获取 FlatList 中所选项目的值。 -- react native