[英]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 语法调用组件,我们想传递
Market
从mapStateToProps
获得的所有道具。 Change改变
this.props.showTokens ? showTokens(this.props)
to到
this.props.ShowTokens ? <ShowTokens {....this.props}/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.