React 16.7 has State Hook, can I use functional component instead of class component in any situation?

react hooks
when to use react hooks
useeffect react
react hooks must be called in a react function component or a custom react hook function
react custom hooks
react-hooks/exhaustive-deps
react hooks nested state
create react app hooks

React 16.7 has State Hook,so I can use function component instead of class component in any situation,is it right? https://reactjs.org/docs/hooks-state.html

Actually,there are some rules when you use hook:Don’t call Hooks inside loops, conditions, or nested functions and Don’t call Hooks from regular JavaScript functions.

You can read these rules and explanation here: https://reactjs.org/docs/hooks-rules.html

And here is the official explaination.

Our goal is for Hooks to cover all use cases for classes as soon as possible. There are no Hook equivalents to the uncommon getSnapshotBeforeUpdate and componentDidCatch lifecycles yet, but we plan to add them soon. It is a very early time for Hooks, so some integrations like DevTools support or Flow/TypeScript typings may not be ready yet. Some third-party libraries might also not be compatible with Hooks at the moment.

How can I use React hooks in React classic `class` component , How do you use the state of another component react? Instead of refactoring the code, you can use a hook such as useState. Hooks don’t work inside class components. Hooks don’t work inside class components. Here’s a comparison between a functional component with the useState hook and a class component with built-in state.

useState hook is intended to be a replacement for this.state in class components:

this.state = { foo: 1, bar: 2 };

becomes either

const [foo, setFoo] = useState(1);
const [bar, setBar] = useState(2);

or

const [state, setState] = useState({ foo: 1, bar: 2 });

In the second case it should be taken into account that setState won't merge state properties with previous state, unless this is done explicitly:

Unlike the setState method found in class components, useState does not automatically merge update objects. You can replicate this behavior by combining the function updater form with object spread syntax:

setState(prevState => ({...prevState, ...updatedValues});

As another answer explains, the limitation is that the order of useState calls should be the same every time functional component is called because the order is the only way for the framework to identify component states.

Problems may appear if the state needs to be accessed outside the component for some reasons, e.g. debugging, testing or specific cases. As the documentation explains, a state in functional component is supposed to be tested by its side effects instead of asserting state directly.

A guide to useState in React, How do you convert class component to functional component in react? React Hooks is a new feature which is coming with React 16.7 and is adding missing pieces of functionality to React’s functional components: In this tutorial we’ll explore both, State Hooks and…

Can a component access the state of another component?, They let you use state and other React features without writing a class. useState returns a pair: the current state value and a function that lets you update it. The initial state argument is only used during the first render. Declaring multiple state variables. You can use the State Hook more than once in a single component:. With useState(documentation) React gives us a way to turn a stateless functional component into a stateful one without turning it into a class. We could already do something similar with recompose and withState but like the previous case, it just wraps the functional component with a Class Component where the state is.

Hooks at a Glance – React, With Hooks, you can create functional components that uses state and look at situations where we have a class-based component with state and learn how With the useState() Hook, we can use state in this functional component. This code is an example of how you can convert a class-based React  In the previous blog, I generated a new React/TypeScript app using the create-react-app tool. In this blog, you'll get familiar with two types of React components, and what a component's state is for. Two types of React components A React component can be declared either as a function or as a class. A functional component…

Five Ways to Convert React Class Components to Functional , React Hooks are a new addition in React 16.8 that let you use state and other React features without writing a class component. In other words, Hooks The initial value of the component is defined using useState . const [count Instead, you can call Hooks from React function components. Let's take look  02. Why React Hook? The first main reason is the Introduce state in a functional component. You know that the states cannot be used in functions. But with hooks, we can use states. Another reason is the handle side effect in react component. It means, now you can use newly introduced state such as useEffect. But do you know for some scenarios

Best Practices With React Hooks, Includes tutorials and code examples on using hooks for state and effects, for context First, we see how to do this with a class-based component using setState : In the functional component example, we have an additional import of useState . the Context API Provider to be functional—and instead of this.​state we will  Items can be added, removed and marked as done — all this is achieved by means of updating the component state. Rewriting the to do list as a Function Component Let’s now try to use React Hooks to implement the exact same behaviour in a function component.

Comments
  • Yep, everything you can do with class components you will be able to do with functional components + hooks.
  • For the reference, here is related question but not full duplicate, stackoverflow.com/questions/53062732/…