我想从Redux存储中获取数据,不是错误:)
在基于功能的组件中,我总是这样
...
import { useSelector } from 'react-redux';
export deafult function App(...){
...
const someData = useSelector(state => state.someData);
...
return (...)
}
...
但是当涉及到基于类的组件时,我希望像这样,但出现错误:(
...
import { useSelector } from 'react-redux';
export default class App extends Component {
constructor(...){
...
const someData = useSelector(state => state.someData);
...
}
render(...)
}
...
答案 0 :(得分:4)
挂钩仅适用于功能组件
代替useSelector,您可以像这样从react-redux传递道具
import React from 'react'
import {connect} from 'react-redux'
class App extends React.Component{
...
//Your data from react-redux can be called like this.props.someData
const someData = this.props.someData;
...
}
const mapStateToProps = state => {
return {
someData: state.someData
}
}
export default connect(mapStateToProps)(App)