Is it possible to use React Hooks outside of functional component, or i have to use mobx or redux?

16,965

Solution 1

Yes, you have to use Redux or MobX to solve this problem. You have to maintain isAuthenticated state in the global state of Redux or MobX. Then make an action that could be named like, toggleAuthState and pass is to the child component and toggle the state from there.

Also you can use functional components for this case. Class based components is not mandatory to use MobX or Redux. If you maintain a HOC as a Container then you can pass the actions and states to the child.

I am showing an example of using a container as a HOC:

// Container
import React from "react"
import * as actions from "../actions" 
import ChildComponent from "../components/ChildComponent"
import { connect } from "react-redux"
import { bindActionCreators } from "redux"

const Container = props => <ChildComponent { ...props } />

const mapStateToProps = state => ({ ...state })

const mapDispatchToProps = dispatch => bindActionCreators(actions, dispatch)

export default connect(mapStateToProps, mapDispatchToProps)(Container)

Then in ChildComponent you can use your states and dispatch actions whenever you need.

Solution 2

Mobx and hooks are very similar in implementation. Both use a render context that is in a sense "global". React ties that render context to the component render context, but Mobx keeps that render context separate. Therefore that means that hooks have to be created within a component render lifecycle (but can sometimes be called outside that context). Mobx-react ties the Mobx render lifecycle to the react lifecycle, triggering a react re-render when observed objects change. So Mobx-react nests the react render context within the Mobx render context.

React internally keeps tracks of hooks by the number of times and order the hook is called within a component render cycle. Mobx, on the other hand, wraps any "observable" object with a proxy that lets the Mobx context know if any of its properties were referenced during a Mobx "run context" (an autorun call, essentially). Then when a property is changed, Mobx knows what "run contexts" care about that property, and re-runs those contexts. This means that anywhere you have access to an observable object you can change a property on it and Mobx will react to it.

For react state hooks, react provides a custom setter function for a state object. React then uses calls to that setter to know when it needs to re-render a component. That setter can be used anywhere, even outside a React render, but you can only create that hook inside a render call, because otherwise react has no way to tell what component to tie that hook to. Creating a hook implicitly connects it to the current render context, and that's why hooks have to be created inside render calls: hook builders have no meaning outside a render call, because they have no way to know what component they are connected to -- but once tied to a component, then they need to be available anywhere. In fact, actions like onClick or a fetch callback don't occur within a render context, although the callback is often created within that context - the action callback happens after react finishes rendering (because javascript is single threaded, so the render function must complete before anything else happens).

Solution 3

Hooks comes as an alternatively to class based components, you should pick up one to your project and stick to it, don't mix it up. there are some motivation for the creation of hooks, as it's better stated at docs: hook motivation.

you can create hook functions apart, but they are meant to be consumed by components. it's something like using HOC (high order component) with class based components.

const myHook = () => {
  [foo, setFoo] = useState('john')
  // use effect for example if you need to run something at state updates
  useEffect(() => { 
    // do something on foo changes
  }, [foo])

  return [foo, setFoo] // returning state and setState you can use them by your component
}

now you have a reusable hook and you can consume at your components:

const myComponent = (props) => {
  [foo, setFoo] = myHook()

  const handleFoo = () => { 
    // some logic
    setFoo(someValue)
  }      
  return (
    <div>
      <span>{foo}<span>
      <button onClick={handleFoo}>click</button>
    </div>
  )
}

obs: you should avoid declare variables as var nowadays, pick const for most, and if it's a value variable (like number) that needs update use let.

Share:
16,965
apporc
Author by

apporc

Updated on June 15, 2022

Comments

  • apporc
    apporc almost 2 years

    I am new to React, and when I was reading about the docs, I found there were two ways to implement React components, functional-based and class-based. I know before React 16.8 it's not possible to manage state in functional components, but after that there is React Hooks.

    The problem is, there seems to be one restriction for React Hooks, they can only be used inside functional components. Take a server-client as an example, which needs to change an isAuthenticated state while 401 received.

    //client.js
    import { useUserDispatch, signOut } from "auth";
    
    export function request(url, args) {
      var dispatch = useUserDispatch();
      return fetch(url, args).then(response => {
      if (response.status === 401) {
        logout(dispatch);
      }
    }
    );
    
    //auth.js
    import React from "react";
    
    var UserStateContext = React.createContext();
    var UserDispatchContext = React.createContext();
    
    function userReducer(state, action) {
      ...
    }
    
    function UserProvider({ children }) {
      var [state, dispatch] = React.useReducer(userReducer, {
        isAuthenticated: false,
      });
    
      return (
        <UserStateContext.Provider value={state}>
          <UserDispatchContext.Provider value={dispatch}>
            {children}
          </UserDispatchContext.Provider>
        </UserStateContext.Provider>
      );
    }
    
    function useUserState() {
      return React.useContext(UserStateContext);
    }
    
    function useUserDispatch() {
      return React.useContext(UserDispatchContext);
    }
    
    function signOut(dispatch) {
      dispatch({});
    }
    
    export { UserProvider, useUserState, useUserDispatch, loginUser, signOut };
    
    

    The client code above will produce error "Hooks can only be called inside of the body of a function component". So maybe I have to move line var dispatch = useUserDispatch() upward to the component where request is called, and pass dispatch as props to request. I feel this is not right, no only request is forced to care about some meaningless(to it) dispatch, but also this dispatch will spread everywhere a component needs to request.

    For class-based components, this.state doesn't solve this problem either, but at least I can use mobx.

    So are there some other ideal ways to solve this problem?