Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

编译到h5端,子组件的 componentDidMount 不会执行 #4356

Closed
fengyun2 opened this issue Sep 1, 2019 · 4 comments
Closed

编译到h5端,子组件的 componentDidMount 不会执行 #4356

fengyun2 opened this issue Sep 1, 2019 · 4 comments
Assignees

Comments

@fengyun2
Copy link

fengyun2 commented Sep 1, 2019

问题描述
编译到h5端,子组件的 componentDidMount 不会执行

复现步骤

https://github.com/ly2011/taro-cnode

import Taro, { Component } from '@tarojs/taro'
import { connect } from '@tarojs/redux'
import { ScrollView } from '@tarojs/components'
import { AtCard, AtLoadMore } from 'taro-ui'
import dayjs from 'dayjs'
import { getWindowHeight } from '@/utils/dom'
import styles from '../index.module.scss'

@connect(({ home, loading }) => ({
 ...home,
 // ...loading,
 isLoad: loading.effects['home/fetchTopics']
}))
class Topics extends Component {
 constructor(props) {
   super(props)
   this.state = {
     status: 'loading'
   }
 }
 loadData = async (page = 1, tab = '') => {
   const { dispatch } = this.props
   dispatch({
     type: 'home/fetchTopics',
     payload: {
       tab,
       page
     },
     callback: (err, data) => {
       if (err) {
         page !== 1 && this.setState({ page: page - 1 })
         return
       } else {
         page !== 1 && this.setState({ page: page })
       }

       if (data && !data.length) {
         this.setState({
           status: 'noMore'
         })
       }
     }
   })
 }
 handleClick = () => {
   this.setState({
     status: 'loading'
   })
   this.loadMore()
 }
 onEndReached = () => {
   this.loadMore()
 }
 onItemPressed = (title, content) => {
   console.log('我被点击了')
 }
 // 加载更多
 loadMore = () => {
   const { data, ownTab: tab } = this.props
   const { page } = data[tab]
   this.loadData(page + 1, tab)
 }
 // 初始化数据
 initData = async () => {
   const { data, ownTab: tab } = this.props
   const { page } = data[tab]
   this.loadData(page, tab)
 }
 componentDidMount() {
   console.log('mounted...')
   this.initData()
 }
 render() {
   const { data, ownTab: tab } = this.props
   if (!tab) return null
   const topics = data[tab].list || []
   const { status } = this.state

   const renderItem = topics.map((item, index) => (
     <AtCard
       key={`${index}-${tab}`}
       note={dayjs(item.last_reply_at).format('YYYY-MM-DD HH:mm')}
       extra={`${item.reply_count}/${item.visit_count}`}
       title={item.author.loginname}
       thumb={item.author.avatar_url}
       className={styles.card}
       onClick={() => {
         this.onItemPressed(item.title, item.content)
       }}
     >
       {item.title}
     </AtCard>
   ))
   return (
     <ScrollView
       scrollY
       style={{ height: getWindowHeight() }}
       lowerThreshold={100}
       enableBackToTop
       onScrollToLower={this.onEndReached}
     >
       {renderItem}
       {<AtLoadMore onClick={this.handleClick} status={status} />}
     </ScrollView>
   )
 }
}

export default Topics

期望行为
编译到h5端,子组件的 componentDidMount 会执行

系统信息

  • 操作系统: [e.g. Windows 10]
  • Taro 版本 [e.g. v.1.3.15]
  • Node.js 版本 [e.g. v12.8.0]
  • 报错平台 [h5]
@taro-bot
Copy link

taro-bot bot commented Sep 1, 2019

欢迎提交 Issue~

如果你提交的是 bug 报告,请务必遵循 Issue 模板的规范,尽量用简洁的语言描述你的问题,最好能提供一个稳定简单的复现。🙏🙏🙏

如果你的信息提供过于模糊或不足,或者已经其他 issue 已经存在相关内容,你的 issue 有可能会被关闭。

Good luck and happy coding~

@luckyadam luckyadam added the H5 label Sep 2, 2019
@taro-bot
Copy link

taro-bot bot commented Sep 2, 2019

CC @Littly

@taro-bot taro-bot bot assigned Littly Sep 2, 2019
@Littly
Copy link
Contributor

Littly commented Sep 4, 2019

constructor里面补上context再试试

@fengyun2
Copy link
Author

fengyun2 commented Sep 5, 2019

@Littly 能详细说下么,不懂如何在 constructor 里面补上context

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants