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

执行redux更新数据报错,component.$store显示值为undefined #2584

Closed
justbecoder opened this issue Mar 26, 2019 · 6 comments
Closed
Assignees

Comments

@justbecoder
Copy link

问题描述
第一次进入小程序,读取storage中的userInfo数据,如果存在执行更新操作,执行更新操作时报component.$store的值是undefined,后续的data就取不到值

复现步骤
[复现问题的步骤]

  1. 第一次进入小程序,读取storage的数据
  2. 执行dispatch操作,更新redux中的userInfo数据
// app.js
@connect(({user}) => {
  return user
}, (dispatch) => {
  return {
    onSetUserInfo () {
      dispatch(setUserInfoAction())
    }
  }
})
componentDidMount () {
    this.props.onSetUserInfo()
  }

// actions/user.js
export const setUserInfoAction = () => {
  // 读取storage中的userInfo,写入到redux中
  return async (dispatch) => {
    try {
      let data = await Taro.getStorage({
        key: 'userInfo'
      })
      // console.log(data)
      dispatch({
        type: SET_USER_INFO,
        value: data.data
      })
    } catch (e) {
      // console.log(111111, e)
      dispatch({
        type: SET_USER_INFO,
        value: null
      })
    }
  }
}

//reducers/user.js
import {INIT_USER_INFO, REMOVE_USER_INFO, SET_USER_INFO} from "../constants/user";
// 设置默认数据
const defaultState = {
  userInfo: null
}

export default function user (state = defaultState, action) {
  // let newState = JSON.parse(JSON.stringify(state))
  switch (action.type) {
    case INIT_USER_INFO:
      return {
        ...state,
        userInfo: action.value
      }
      break
    case SET_USER_INFO:
      console.log(action, '实际打入的数据')
      if (action.value) {
        return {
          ...state,
          userInfo: action.value
        }
      } else {
        return state
      }

      break
    case REMOVE_USER_INFO:
      return {
        ...state,
        userInfo: null
      }
      break
    default:
      return state
  }
}

期望行为
中间过程为什么会报错

报错信息

image
image

系统信息

Taro v1.2 及以上版本已添加 taro info 命令,方便大家查看系统及依赖信息,运行该命令后将结果贴下面即可。

  • 操作系统: [macOS High Sierra 10.13.6]
  • Taro 版本 [v1.2.15]
  • Node.js 版本 [v.10.15.0]
  • 报错平台 [weapp]
@taro-bot
Copy link

taro-bot bot commented Mar 26, 2019

欢迎提交 Issue~

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

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

Good luck and happy coding~

@luckyadam
Copy link
Member

能否提供一个可以运行的 demo

@Chen-jj Chen-jj self-assigned this Mar 27, 2019
@taro-bot
Copy link

taro-bot bot commented Mar 27, 2019

Hello~

您的问题我们无法复现。如果有空的话还请拔冗提供一个简单的复现 demo,否则这个 issue 将在 15 天后被自动关闭。

如果您在这 15 天中更新更多信息自动关闭的流程会自动取消,如有其他问题也可以发起新的 Issue。

Good luck and happy coding~

@Chen-jj
Copy link
Contributor

Chen-jj commented Mar 27, 2019

@justbecoder 复现不了。这句话也没看懂,你给的代码也并没有体现: “如果存在执行更新操作,执行更新操作时报component.$store的值是undefined,后续的data就取不到值”。

像你提供的代码一样在 app.js dispatch 一个 action 初始化 userInfo 并没有报错,即使后面再在页面 dispatch 一个 action 修改 userInfo 也没有问题。建议将 CLI 和依赖都升级到最新版本后再试试。

如果还有问题,可以提供完整的 demo 我们跑一下。

@lanxkk
Copy link

lanxkk commented Apr 4, 2019

我也遇到了同样的问题,报错:TypeError: Cannot read property 'data' of undefined

感觉像是在 app.js 里面不能使用 @connect ,在其他页面使用同样的代码,就不会报错。

@julenwang
Copy link

我也遇到了同样的问题,报错:TypeError: Cannot read property 'data' of undefined

感觉像是在 app.js 里面不能使用 @connect ,在其他页面使用同样的代码,就不会报错。

很有帮助,感谢

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

5 participants