简体   繁体   English

React props 给我错误:无法解构“props”的属性“dispatch”,因为它未定义

[英]React props giving me error : Cannot destructure property 'dispatch' of 'props' as it is undefined

I dont understand why I'm getting this error because they are all selectors I am using in other components.我不明白为什么我会收到这个错误,因为它们都是我在其他组件中使用的选择器。 And this component is basically a copy of another one.而这个组件基本上是另一个组件的副本。 I was under the impression that the props in { } = props where coming from my imported selectors.我的印象是 { } = props 中的道具来自我导入的选择器。

But i'm getting this error;但是我收到了这个错误;

Cannot destructure property 'exchange' of 'props' as it is undefined. And it's the same error for any props i'm trying to pass to { } = props .对于我试图传递给{ } = props任何道具,它都是相同的错误。

import React, { Component } from 'react'
import { connect } from 'react-redux'
import { OverlayTrigger, Tooltip } from 'react-bootstrap'
import Spinner from './Spinner'
import {
    allTokensLoadedSelector,
    allTokensSelector,
    web3Selector,
    exchangeSelector
} from '../store/selectors'
import { loadToken } from '../store/interactions'

const showTokens = (data, props) => {
    const { dispatch, exchange, web3 } = props
    return (

    <tbody>
        { data.allTokens.map( (token) => {
            return(
            <OverlayTrigger trigger={['hover', 'focus' ]} key={token.tokenId} placement='top' overlay={ <Tooltip className="tooltip" id={token.TokenId}>
                {`Click to load ${token.symbol} market`}
                    </Tooltip>
                }
                >
                <tr key={token.tokenId} className="order-book-order" onClick={(e)=> loadToken(web3, exchange, token.tokenAddress, dispatch)}
                    >
                    <td>{token.symbol}/ETH</td>
                </tr>
            </OverlayTrigger>
            )
        })}
    </tbody>
    )
}

class Market extends Component {
    render() {
        console.log(this.props.exchange)
        return (
        <div className="vertical">
            <div className="card bg-transparent text-white">
                <div className="card-header">
                    Market
                </div>
                <div className="card-body order-book">
                    <table className="table bg-transparent text-white table-sm small">
                        { this.props.showTokens ? showTokens(this.props) :
                        <Spinner type='table' /> }
                    </table>
                </div>
            </div>
        </div>
        )
    }
}

function mapStateToProps(state) {
    const tokensLoaded = allTokensLoadedSelector(state)
    return {
        showTokens: tokensLoaded,
        allTokens: allTokensSelector(state),
        web3: web3Selector(state),
        exchange: exchangeSelector(state),
    }
}

export default connect(mapStateToProps)(Market);

So where do I have to look to fix this I dont get it.所以我必须在哪里解决这个问题,我不明白。

Right now you're in this weird middle ground where showTokens is just a function but you want it to be a function component, so let's make it a function component.现在你处于这个奇怪的中间地带,其中showTokens只是一个函数,但你希望它是一个函数组件,所以让我们把它变成一个函数组件。

Since it's now a component, it needs a CamelCase name.因为它现在是一个组件,所以它需要一个 CamelCase 名称。 It needs to take one argument which is an object of props, and we can destructure those props immediately.它需要一个参数,它是 props 的对象,我们可以立即解构这些 props。 Change改变

const showTokens = (data, props)

to

const ShowTokens = ({dispatch, exchange, web3, allTokens})

allTokens is now a top-level prop (not a property of data), so change allTokens现在是顶级道具(不是数据的属性),因此更改

data.allTokens.map

to

allTokens.map

We want to call the component using JSX syntax, and we want to pass through all of the props which Market got from mapStateToProps .我们想使用 JSX 语法调用组件,我们想传递MarketmapStateToProps获得的所有道具。 Change改变

this.props.showTokens ? showTokens(this.props)

to

this.props.ShowTokens ? <ShowTokens {....this.props}/>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 React CRUD 应用程序中的错误:TypeError:无法解构“this.props.event”的属性“id”,因为它未定义 - Error in React CRUD app : TypeError: Cannot destructure property 'id' of 'this.props.event' as it is undefined TypeError:无法解构“this.props”的属性“凭据”,因为它未定义 - TypeError: Cannot destructure property 'credentials' of 'this.props' as it is undefined 未捕获的 TypeError:无法解构“props”的属性“recipes”,因为它未定义 - Uncaught TypeError: Cannot destructure property 'recipes' of 'props' as it is undefined “无法解构'this.props.reservation'的属性'reservations',因为它是未定义的”并过滤它ReactJs - “Cannot destructure property 'reservations' of 'this.props.reservation' as it is undefined” and filter it ReactJs TypeError:无法解构“props.message”的属性“text”,因为它未定义 - TypeError: Cannot destructure property 'text' of 'props.message' as it is undefined 无法解构“this.props.data”的属性“聊天”,因为它未定义 - Cannot destructure property 'chats' of 'this.props.data' as it is undefined 错误:未捕获 [TypeError:无法解构“props.value”的属性“匹配”,因为它未定义。] - Error: Uncaught [TypeError: Cannot destructure property 'match' of 'props.value' as it is undefined.] React-redux:无法使用connect获取作为道具的商店,无法读取未定义错误的属性“ props” - React-redux: cannot fetch the store as props using connect, Cannot read property 'props' of undefined error 无法读取未定义反应的属性道具 - cannot read property props of undefined react React - TypeError:无法读取未定义的属性“道具” - React - TypeError: Cannot read property 'props' of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM