【问题标题】:React-redux useSelector used in a class component在类组件中使用 React-redux useSelector
【发布时间】:2022-12-09 11:07:09
【问题描述】:

嗨,我是 React 的新手,正在尝试在无法正常工作的类组件中获取 useSelector。任何帮助将不胜感激。

useSelector 看起来像这样:

const userLogin = useSelector(state => state.userLogin);
const {userInfo} = userLogin;

我正试图通过它:

export class StatusReport extends Component{

    constructor(props) {
        super(props);

        this.state = {
            // Active Work Orders clients
            AWOS: [],
            clientIsLoading: true,
        }
    }


    componentDidMount() {
        axios.get(process.env.REACT_APP_API+'status_reports/',{
            headers:{
                "Accept":'application/json',
                "Content-Type":'application/json'
            },
        })
            .then(response=>{
                this.setState({AWOS: response.data});
                this.setState({clientIsLoading: false});
            })
    }

    render() {

        const {clientIsLoading} = this.state;

        if (clientIsLoading){
            return <div className={'loadingSpinner'} >Loading...<br/> <ClockLoader color="#0d00ff" /></div>
        }

        return (

我尝试过不同的方式通过状态

【问题讨论】:

标签: reactjs react-hooks react-redux


【解决方案1】:

useSelector 必须在函数组件内部使用。对于类组件,也许您想使用connect。参考:https://react-redux.js.org/api/connect

【讨论】:

    猜你喜欢
    • 2019-11-29
    • 2020-05-17
    • 1970-01-01
    • 2022-11-14
    • 2021-09-03
    • 2019-12-19
    • 1970-01-01
    • 2022-12-01
    • 2020-12-06
    相关资源
    最近更新 更多