gpt4 book ai didi

javascript - 如何在基于类的组件中使用 `useSelector`? react ,还原

转载 作者:行者123 更新时间:2023-12-04 00:53:33 25 4
gpt4 key购买 nike

我想从我的 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(...)
}
...

最佳答案

Hooks 仅适用于功能组件
你可以像这样从 react-redux 传递 Prop ,而不是 useSelector

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)

关于javascript - 如何在基于类的组件中使用 `useSelector`? react ,还原,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64491841/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com