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端,第二级页面更新redux数据后,页面go back时,上一级页面的组件里的redux数据没有及时更新显示。 #4316

Closed
numeny opened this issue Aug 28, 2019 · 4 comments
Labels
T-h5 Target - 编译到 H5 V-1 Version - 1.x
Projects

Comments

@numeny
Copy link

numeny commented Aug 28, 2019

问题描述
H5端,第二级页面更新redux数据后,页面go back时,上一级页面的组件里的redux数据没有及时更新显示。

复现步骤

  1. 按Taro里的使用redux的例子建立工程,Taro的redux实例:http://taro-docs.jd.com/taro/docs/redux.html
    包括:目录src/actions,src/constants,src/reducers,src/store目录下的,以及src/app.tsx按照Taro的redux示例修改。
  2. src/pages/index.tsx, src/pages/mycomp.tsx, src/pages/page2.tsx目录修改按照下面的代码修改。
  3. 打开的index页面;
  4. 点击button,执行this.toPage2(),进入Page2页面
  5. 在Page2页面,点击Page2里的MyComp组件里的加或者减的按钮,本页面的redux数据能够及时更新。
  6. 回退到上一个index页面
    问题:index页面里的MyComp组件里redux数据无法更新显示。只有再点击index页面里的加/减按钮时,才更新显示。
    这个问题只有在H5端才有,在微信小程序端没有。
// src/pages/index/index.tsx
import Taro, { Component, Config } from '@tarojs/taro'
import { View, Text, Button } from '@tarojs/components'
import './index.scss'

import Mycomp from '../mycomp/mycomp'

import { connect } from '@tarojs/redux'

import { add, minus, asyncAdd } from '../../actions/counter'

@connect(({ counter }) => ({
    counter
}), (dispatch) => ({
    add () {
        dispatch(add())
    },
    dec () {
        dispatch(minus())
    },
    asyncAdd () {
        dispatch(asyncAdd())
    }
}))

export default class Index extends Component {

  toPage2 = () => {
      Taro.navigateTo({
          url: '/pages/page2/page2',
      })
  }

  render () {
    return (
      <View className='index'>
        <Text>Page 1</Text>
        <Button className='add_btn' onClick={this.props.add}>+</Button>
        <Button className='dec_btn' onClick={this.props.dec}>-</Button>
        <Button className='dec_btn' onClick={this.props.asyncAdd}>async</Button>
        <View>{this.props.counter.num}</View>
        <View>following is MyComp</View>
        <Mycomp />
        <View>To Page2</View>
        <Button className='dec_btn' onClick={this.toPage2}>To Page2</Button>
      </View>
    )
  }
}

// src/pages/page2/page2.tsx
import Taro, { Component, Config } from '@tarojs/taro'
import { View, Text, Button } from '@tarojs/components'

import Mycomp from '../mycomp/mycomp'

import { connect } from '@tarojs/redux'

import { add, minus, asyncAdd } from '../../actions/counter'

@connect(({ counter }) => ({
    counter
}), (dispatch) => ({
    add () {
        dispatch(add())
    },
    dec () {
        dispatch(minus())
    },
    asyncAdd () {
        dispatch(asyncAdd())
    }
}))

export default class Page2 extends Component {
  render () {
    return (
      <View className='index'>
        <Text>Page 2</Text>
        <Button className='add_btn' onClick={this.props.add}>+</Button>
        <Button className='dec_btn' onClick={this.props.dec}>-</Button>
        <Button className='dec_btn' onClick={this.props.asyncAdd}>async</Button>
        <View>{this.props.counter.num}</View>
        <View>following is MyComp</View>
        <Mycomp />
      </View>
    )
  }
}

// src/pages/mycomp/mycomp.tsx
import Taro, { Component, Config } from '@tarojs/taro'
import { View, Text, Button } from '@tarojs/components'

import { connect } from '@tarojs/redux'

import { add, minus, asyncAdd } from '../../actions/counter'

@connect(({ counter }) => ({
    counter
}), (dispatch) => ({
    add () {
        dispatch(add())
    },
    dec () {
        dispatch(minus())
    },
    asyncAdd () {
        dispatch(asyncAdd())
    }
}))

export default class Mycomp extends Component {
  render () {
    return (
      <View className='index'>
        <Text>My Comp!</Text>
        <Button className='add_btn' onClick={this.props.add}>2+</Button>
        <Button className='dec_btn' onClick={this.props.dec}>2-</Button>
        <Button className='dec_btn' onClick={this.props.asyncAdd}>2async</Button>
        <View>{this.props.counter.num}</View>
      </View>
    )
  }
}

// src/app.tsx
import Taro, { Component, Config } from '@tarojs/taro'
import Index from './pages/index'

import './app.scss'

import { Provider } from '@tarojs/redux'
import configStore from './store'

const store = configStore()

// 如果需要在 h5 环境中开启 React Devtools
// 取消以下注释:
// if (process.env.NODE_ENV !== 'production' && process.env.TARO_ENV === 'h5')  {
//   require('nerv-devtools')
// }

class App extends Component {

  /**
   * 指定config的类型声明为: Taro.Config
   *
   * 由于 typescript 对于 object 类型推导只能推出 Key 的基本类型
   * 对于像 navigationBarTextStyle: 'black' 这样的推导出的类型是 string
   * 提示和声明 navigationBarTextStyle: 'black' | 'white' 类型冲突, 需要显示声明类型
   */
  config: Config = {
    pages: [
      'pages/index/index',
      'pages/page2/page2'
    ],
    window: {
      backgroundTextStyle: 'light',
      navigationBarBackgroundColor: '#fff',
      navigationBarTitleText: 'WeChat',
      navigationBarTextStyle: 'black'
    }
  }

  componentDidMount () {}

  componentDidShow () {}

  componentDidHide () {}

  componentDidCatchError () {}

  // 在 App 类中的 render() 函数没有实际作用
  // 请勿修改此函数
  render () {
    return (
      <Provider store={store}>
        <Index />
      </Provider>
    )
  }
}

Taro.render(<App />, document.getElementById('app'))


// ```js
// 代码放这里(前后的 "```" 有助 Mardown 整理代码格式和加入代码高亮)
// ```

// 提供完整可复现的代码和整理好代码格式,有助于我们快速定位问题,节省你我时间。
// 代码提供不全或代码格式混乱的 issues 【有可能被忽略】,烦请各位注意。

期望行为
H5端,第二级页面更新redux数据后,页面go back时,上一级页面的组件里的redux数据应该及时更新显示。

报错信息

系统信息
👽 Taro v1.3.13

Taro CLI 1.3.13 environment info:
System:
OS: Linux 4.10 Ubuntu 16.04.3 LTS (Xenial Xerus)
Shell: 4.3.48 - /bin/bash
Binaries:
Node: 8.9.4 - ~/.nvm/versions/node/v8.9.4/bin/node
npm: 5.6.0 - ~/.nvm/versions/node/v8.9.4/bin/npm
npmPackages:
@tarojs/components: 1.3.0-beta.3 => 1.3.0-beta.3
@tarojs/plugin-babel: ^1.3.0 => 1.3.14
@tarojs/plugin-sass: ^1.3.0 => 1.3.14
@tarojs/router: 1.3.0-beta.3 => 1.3.0-beta.3
@tarojs/taro: 1.3.0-beta.3 => 1.3.0-beta.3
@tarojs/taro-h5: 1.3.0-beta.3 => 1.3.0-beta.3
@tarojs/taro-weapp: ^1.3.0 => 1.3.9
@tarojs/webpack-runner: ^1.3.0 => 1.3.9
nerv-devtools: ^1.4.0-beta.3 => 1.4.3
nervjs: ^1.4.0-beta.3 => 1.4.3

  • 操作系统: Ubuntu 16.04.3
  • Taro 版本 1.3.13
  • Node.js 版本 1.4.3
  • 报错平台:h5

补充信息

@taro-bot
Copy link

taro-bot bot commented Aug 28, 2019

欢迎提交 Issue~

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

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

Good luck and happy coding~

@luckyadam
Copy link
Member

回退之后需要在 上一个index页面 的 componentDidShow 方法里进行处理

@shengbowen
Copy link

这个问题怎么解决,这个很尴尬啊~

@moseszhou
Copy link
Contributor

回退之后需要在 上一个index页面 的 componentDidShow 方法里进行处理

这是什么鬼解决方案..., 岂不是我需要在componentDidShow中调用组件的forceUpdate,去强制更新。

@ZakaryCode ZakaryCode added T-h5 Target - 编译到 H5 V-1 Version - 1.x labels Jan 15, 2022
@ZakaryCode ZakaryCode added this to Padding in H5 via automation Jan 15, 2022
@ZakaryCode ZakaryCode moved this from Padding to Taro 1.+、2.+ in H5 Jan 15, 2022
H5 automation moved this from Taro 1.+、2.+ to Done Aug 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T-h5 Target - 编译到 H5 V-1 Version - 1.x
Projects
Archived in project
H5
  
Done
Development

No branches or pull requests

5 participants