小编Nat*_*haC的帖子

如何在react.js中滚动显示/隐藏组件

我只是在用户向下滚过标题时尝试制作页脚横幅,并在用户向上滚动到顶部时隐藏它.我对如何实现这个的想法是在页面的顶部组件中为scroll事件添加一个事件监听器.这是我目前的代码.但它不起作用:

componentDidMount() {
  this.nv.addEventListener('scroll', this.handleScroll);
}

handleScroll(e) {
  if(window.pageYOffset > 50 && window.pageYOffset < 900) {
    console.log('scrolled');
    this.setState({
      showBanner: true,
    });
  } else {
    this.setState({
      showBanner: false,
    });
  }
}
Run Code Online (Sandbox Code Playgroud)

顶级组件的渲染方法:

render() {
  return (
    <div ref={elem => this.nv = elem}>
      <Header/>
      <Body
        userInfo={this.props.userInfo}
        history={this.props.history}
      />
      {
        this.state.showBanner && <Banner/>
        || null
      }
    </div>
  )
}
Run Code Online (Sandbox Code Playgroud)

我没有看到任何日志,setState滚动时显然没有调用.

关于如何实现预期目标的任何建议?

*UPDATE* 更改componentDidMount为在窗口上添加事件监听器,现在它正在工作.

componentDidMount() {
  window.addEventListener('scroll', this.handleScroll);
}
Run Code Online (Sandbox Code Playgroud)

html javascript css reactjs

6
推荐指数
1
解决办法
5028
查看次数

如何使用react native和react-native-fetch-blob在iOS上下载和检索本地文件?

我正在使用react-native-fetch-blob来下载mp3文件,然后再播放它们.问题是RNFetchBlob.fs.dirs.DocumentDir当我关闭应用程序并重新启动时,实际的更改路径,这意味着我无法使用下载文件后立即存储的绝对文件路径,在下次运行应用程序时检索文件.

这是我下载和存储文件路径的代码:

export function downloadAudio(urlArray) { //download every section in the urlArray
  return (dispatch) => {
    Promise.all(urlArray.map(section => {
      dispatch(startDownload(section.section_id))
      console.log('download start for id = ',section.section_id ) //start download
      return RNFetchBlob
      .config({
        path: RNFetchBlob.fs.dirs.DocumentDir + '/courseSections/' + section.section_id + '.mp3',
        appendExt: 'mp3'
      })
      .fetch('GET', section.section_url, {
        'Cache-Control': 'no-store'
      })
      .progress({ count: 10 }, (received, total) => {
        console.log(`progress for section ${section.section_id}: `, Math.floor(received/total*100), '%')
        dispatch(downloadProgress({id: section.section_id, progress: Math.floor(received/total*100)/100}))
      })
      .then(res => {
        console.log(`section ${section.section_id} is saved to: `, …
Run Code Online (Sandbox Code Playgroud)

iphone ios react-native react-native-fetch-blob

5
推荐指数
1
解决办法
4977
查看次数