Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redux on functional components in Reactjs (web)

I am writing this question cause I would like to ask you for some help in how to use the redux on my functional components. I had a look at other examples with React components but I cannot understand how to get the "store" value in functional components.

My idea is to use my

store.getState()

To check states and interact with the UI, inside my functional component but I cannot make it happen.

Any help please ?

For example, a functional component :

import React from 'react';
import withStyles from 'isomorphic-style-loader/lib/withStyles';
import s from './Header.css';
import { Navbar, Nav } from 'react-bootstrap';
import HeaderMenu from '../HeaderMenu';
import cx from 'classnames';

function Header() {
  return (
    <Navbar fluid fixedTop id="Header" className={s.navContainer}>
      <Nav block className={cx(s.HeaderTitle, s.hideOnSmall)}>Project title</Nav>
      <HeaderMenu />
    </Navbar>
  );
}

export default withStyles(s)(Header);

How can I use the "store" object inside my Header component ? It works on my App component, just I don't know how to use it within my components.

My questions are:

  • Should I use actions for retrieving the state instead ??
  • Should I pass the store object component to the component properties?

Thanks in advance!

EDIT :

I am using https://github.com/kriasoft/react-starter-kit with the redux branch https://github.com/kriasoft/react-starter-kit/tree/feature/redux

like image 444
themazz Avatar asked Jul 11 '16 13:07

themazz


People also ask

Can you use Redux with functional components?

Using Hooks in a React Redux App​ From there, you may import any of the listed React Redux hooks APIs and use them within your function components.

How can I call Redux action from functional component?

There are two ways to dispatch actions from functional components: Using mapDispatachToProps function with connect higher order component, same as in class based components. Using useDispatch hook provided by react-redux . If you want to use this hook, then you need to import it from the react-redux package.

How do I access Redux functional component?

The selector will be run whenever the function component renders. useSelector() will also subscribe to the Redux store, and run your selector whenever an action is dispatched. You may call useSelector() multiple times within a single function component.

Is React Redux functional?

React is popular for its ability to write functional components. With the React-Redux release, we can use the Hook API to manage the state without using the higher-order components. Thus, we no longer need to add extra components to the component tree.


1 Answers

As of version 7.x react-redux now has hooks for functional components.

Header.jsx

import React from 'react';
import withStyles from 'isomorphic-style-loader/lib/withStyles';
import s from './Header.css';
import { Navbar, Nav } from 'react-bootstrap';
import HeaderMenu from '../HeaderMenu';
import cx from 'classnames';
import { useSelector } from 'react-redux'

function Header() {
  const store = useSelector(store => store)
  return (
    <Navbar fluid fixedTop id="Header" className={s.navContainer}>
      <Nav block className={cx(s.HeaderTitle, s.hideOnSmall)}>Project title</Nav>
      <HeaderMenu />
    </Navbar>
  );
}

export default withStyles(s)(Header);

index.js

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import { Provider } from 'react-redux'
import store from './store'

ReactDOM.render(
    <Provider store={store}>
        <App />
    </Provider>,
    document.getElementById('root')
);
like image 83
Miguel Coder Avatar answered Sep 20 '22 07:09

Miguel Coder