admin管理员组

文章数量:1331230

I wanna get the data from my Redux storage, not an error :)

In a function based ponent I always do like this

...

import { useSelector } from 'react-redux';

export deafult function App(...){
  ...

  const someData = useSelector(state => state.someData);

  ...
  return (...)
}
...

But when it es to a class based ponent I want smth like this, but I get an error :(

...
import { useSelector } from 'react-redux';

export default class App  extends Component {
  constructor(...){
    ...
    
    const someData = useSelector(state => state.someData);

    ...
  }

  render(...)
}
...

I wanna get the data from my Redux storage, not an error :)

In a function based ponent I always do like this

...

import { useSelector } from 'react-redux';

export deafult function App(...){
  ...

  const someData = useSelector(state => state.someData);

  ...
  return (...)
}
...

But when it es to a class based ponent I want smth like this, but I get an error :(

...
import { useSelector } from 'react-redux';

export default class App  extends Component {
  constructor(...){
    ...
    
    const someData = useSelector(state => state.someData);

    ...
  }

  render(...)
}
...

Share Improve this question edited Oct 22, 2020 at 23:48 asked Oct 22, 2020 at 23:23 user14497276user14497276
Add a ment  | 

1 Answer 1

Reset to default 7

Hooks only works with functional ponents

Instead of useSelector you can pass you props from react-redux like this

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)

本文标签: javascriptHow to use useSelector in a class based component ReactreduxStack Overflow