7

I am new to React and Redux and as we know, it is best to use class component for those components that have state and the question I would like to ask is that Is it recommended to use functional component for components that have connection and interaction with Redux store since those components that interact with store do not have state locally.

4
  • Why they dont have a local state? Commented Oct 8, 2019 at 5:24
  • @DennisVash, hi Dennis, they do not have local state since state is located in redux store and component itself does not have state locally but in redux store, Should we use functional component in such case? Commented Oct 8, 2019 at 6:15
  • @DennisVash, please Dennis I really need your help:( Commented Oct 8, 2019 at 7:34
  • I added an answer that might help Commented Oct 8, 2019 at 7:50

3 Answers 3

6

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

const store = useSelector(store => store)

So that we can use functional component with redux store like class component.

please check below link to get more idea about hooks

https://react-redux.js.org/next/api/hooks

Sign up to request clarification or add additional context in comments.

4 Comments

thank you for your kind reply, what if I do not want use hooks
Then you will have to use class component to get connect to the redux store. Because without hooks you can not connect to redux store
we can not say functional component is not appropriate, it totally depend on your requirement as it has hooks so whatever we do in class component we can do in functional now.
very correct !!. Although to be more descriptive, it will be const counter = useSelector(state => state.counter)
4

It's perfectly fine to connect functional components to redux store.

Functional components don't have a state is not completely correct with hooks. You can add state to functional component with hooks.

Answering your question, you can connect functional component with redux store like below.

import React from "react";
import ReactDOM from "react-dom";
import { createStore } from "redux";
import { Provider, connect } from "react-redux";

const reducers = (state = 0, action) => {
  switch (action.type) {
    case "INCREMENT":
      return state + 1;
    case "DECREMENT":
      return state - 1;
    default:
      return state;
  }
};

const store = createStore(reducers, 0);

const App = ({ count, handleIncrement, handleDecrement }) => {
  return (
    <div>
      <button onClick={handleIncrement}>+</button>
      <h4>{count}</h4>
      <button onClick={handleDecrement}>-</button>
    </div>
  );
};

const mapStateToProps = state => {
  return { count: state };
};

const mapDispatchToProps = dispatch => {
  return {
    handleIncrement: () => {
      dispatch({ type: "INCREMENT" });
    },
    handleDecrement: () => {
      dispatch({ type: "DECREMENT" });
    }
  };
};

const ConnectedApp = connect(
  mapStateToProps,
  mapDispatchToProps
)(App);

ReactDOM.render(
  <Provider store={store}>
    <ConnectedApp />
  </Provider>,
  document.getElementById("root")
);

Comments

3

Is it recommended to use functional components for components that have connection and interaction with the Redux store since those components that interact with the store do not have state locally.

Yes, it is recommended to use functional components with redux, and there is a way to have a local state in a functional component.


Why functional components are recommended?

The react ecosystem moves toward the use of hooks which means standardize the functional components.

As stated in docs about uses of hooks or classes:

In the longer term, we expect Hooks to be the primary way people write React components.


How to have a local state in functional components with redux?

Redux introduced redux-hooks API which gives functional components the ability to use local component state and allows to subscribe to the Redux store without wrapping your components with connect().

// Creating a store
const store = createStore(rootReducer)

ReactDOM.render(
  <Provider store={store}>
    <CounterComponent />
  </Provider>,
  document.getElementById('root')
)

// CounterComponent.jsx Selector example
import React from 'react'
import { useSelector } from 'react-redux'

export const CounterComponent = () => {
  // Using the store localy with a selector.
  const counter = useSelector(state => state.counter)
  return <div>{counter}</div>
}

// CounterComponent.jsx Dispatch Example
import React from 'react'
import { useDispatch } from 'react-redux'

export const CounterComponent = ({ value }) => {
  // Dispatching an action
  const dispatch = useDispatch()

  return (
    <div>
      <span>{value}</span>
      <button onClick={() => dispatch({ type: 'increment-counter' })}>
        Increment counter
      </button>
    </div>
  )
}

// CounterComponent.jsx Referencing the store example
import React from 'react'
import { useStore } from 'react-redux'

export const CounterComponent = ({ value }) => {
  const store = useStore()

  // EXAMPLE ONLY! Do not do this in a real app.
  // The component will not automatically update if the store state changes
  return <div>{store.getState()}</div>
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.