NervJS / taro

开放式跨端跨框架解决方案,支持使用 React/Vue/Nerv 等框架来开发微信/京东/百度/支付宝/字节跳动/ QQ 小程序/H5/React Native 等应用。 https://taro.zone/
https://docs.taro.zone/
Other
35.31k stars 4.76k forks source link

无法在 mapStateToProps 获取到 ownProps #1746

Closed bmcciscoding closed 5 years ago

bmcciscoding commented 5 years ago

问题描述

我创建了一个组件A, 我想通过 方式传递属性,但是我同时也使用 redux 的 connect 来创建组件,现在我无法再 mapStateToProps 的第二个参数 ownProps 拿到 p1

复现步骤

传递属性 p1

render () {
    return (
      <Provider store={store}>
        <Index p1='test' />
      </Provider>
    )
  }

期望在通过 ownProps.p1 拿到传递过来的值

// todolist.js
const mapStateToProsp = (state, ownProps) => {
  return {
    ...state.todoListReducer,
    p1: ownProps.p1
  }
}

导出

// totodlist.js
export default connect(mapStateToProsp, mapDispatchToProps)(Index)

期望行为

在 mapStateToProps 中第二个参数 ownProps 拿到传递过来的属性, 或者我该如何写才能拿到呢?

报错信息

p1: undefined

系统信息

  • 报错平台:weapp

补充信息

和 Redux 的文档不一致,在 Taro-Redux 中也没有找到相关的说明

taro-bot[bot] commented 5 years ago

欢迎提交 Issue~

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

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

Good luck and happy coding~

luckyadam commented 5 years ago

在群里的回复,以及文档,还有生成的模板代码注释中,都有提及,app 的 render 函数是个语法糖,不会起任何作用,我不明白你为什么还要在 app 的 render 里给页面传 props

image

taro-bot[bot] commented 5 years ago

Hello~

您的问题楼上已经有了确切的回答,如果没有更多的问题这个 issue 将在 15 天后被自动关闭。

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

Good luck and happy coding~

bmcciscoding commented 5 years ago

感谢你的回复

开发环境

因为我的需求其实是想给子组件传递属性,例如这里的 Todo 是我自定义的一个组件

// todo.js
import Taro, { Component } from '@tarojs/taro'
import { View, Button, Text } from '@tarojs/components'
import { connect } from '@tarojs/redux'

const mapStateToProps = (state, ownProps) => {
    console.log('state:', state)
    console.log('ownProps:', ownProps) // 打印 {}
    return {
        todo: ownProps.todo
    }
}

const mapDispatchToProps = (dispatch) => {
    return {

    }
}

class Todo extends Component {

  render () {

    return (
      <View className='index'>
        <Text>title: {this.props.todo.title}</Text>
      </View>
    )
  }
}

// export default Todo // 这种方式可以拿到属性
export default connect(mapStateToProps, mapDispatchToProps)(Todo)
// todolist.js
import Taro, { Component } from '@tarojs/taro'
import { View, Button, Text } from '@tarojs/components'
import { connect } from '@tarojs/redux'

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

import './index.css'

import Todo from '../../components/todo/todo'

@connect(({ counter }) => ({
  counter
}), (dispatch) => ({
  add () {
    dispatch(add())
  },
  dec () {
    dispatch(minus())
  },
  asyncAdd () {
    dispatch(asyncAdd())
  }
}))
class Index extends Component {

  config = {
    navigationBarTitleText: '首页'
  }

  constructor(props) {
    super(props)

    this.state = {
       todolist: [{
         title: 'state title'
       }]
    }
  }

  render () {

    const todolist = this.state.todolist.map((data, index) => {
        return <Todo key={index} todo={data}></Todo>
    })

    const todoModel = { title: 'I am title' }
    return (
      <View className='index'>
        <Todo todo={todoModel}></Todo>
        { todolist }
      </View>
    )
  }
}
export default Index

但是这样我依然无法拿到 todo。所以我才会在 App 入口中测试一个最小的例子。 所以我想弄明白是不是通过 connect 的组件都无法通过 ownProps 拿到传递的值?

bmcciscoding commented 5 years ago

编译成 h5 是没问题的,但是小程序会出现这样的情况

yuche commented 5 years ago

close via https://github.com/NervJS/taro/commit/c3dc5d1c8fbfce0508f4d0219498937d26712e7f