https://linen.dev logo
Join DiscordCommunities
Powered by
# react
  • u

    user

    06/28/2019, 12:27 PM
    ok, so no point then?
  • u

    user

    06/28/2019, 12:27 PM
    because I feel like I'm just not getting it
  • u

    user

    06/28/2019, 12:32 PM
    I think just adding a few nice life cycle events would achieve all the things that hooks currently make easier
  • u

    user

    06/28/2019, 12:35 PM
    "all the things" = the use cases I've seen so far that are less verbose with hooks vs. life cycle events
  • u

    user

    06/28/2019, 1:31 PM
    i think that hooks can be good only for composition
  • u

    user

    06/28/2019, 1:45 PM
    I think it's not worth a flamewar.
  • u

    user

    06/28/2019, 2:02 PM
    @francescoagati do you have an example of composition with hooks that can't be easily done with classes?
  • u

    user

    06/28/2019, 3:11 PM
    https://rangle.io/blog/refactor-to-react-hooks-not-classes/
  • u

    user

    06/28/2019, 3:12 PM
    but i think that in many case class and hooks are equivalent
  • u

    user

    06/28/2019, 4:41 PM
    yeah, I mean the problem is simply that react life cycle events are not very well designed and instead of improving that, React team seems hell-bent to not use classes because FP is the solution to everything This is the proposed method:
    Copy code
    js
    export function Layout(props) {
      const [isSidebarExpanded, setSideBarExpanded] = useState(true);
    
      function handleRouteChange(url) {
        setSideBarExpanded(true);
      }
    
      useEffect(function routeChangeComplete() {
        Router.events.on('routeChangeComplete', handleRouteChange);
        return () => Router.events.off('routeChangeComplete', handleRouteChange);
      },[]);
      // ...
    Here's the same in coconut with evil, evil classes:
    Copy code
    haxe
    class Layout extends View {
      @:state isSidebarExpanded:Bool = true;
    
      function handleRouteChange(url) {
        isSidebarExpanded = true;
      }
    
      function viewDidMount() {
        Router.events.on('routeChangeComplete', handleRouteChange);
        beforeUnmounting(() -> Router.events.off('routeChangeComplete', handleRouteChange));
      });
      // ...
    This could practically look exactly the same in React, if they were to work a bit on the life cycle events
  • u

    user

    06/28/2019, 4:44 PM
    Hooks are a pure JS solution which doesn't require fancy compiler magic - only a lot of React magic :D
  • u

    user

    06/28/2019, 4:46 PM
    (and a lot of closures garbage to collect)
  • u

    user

    06/28/2019, 4:46 PM
    The JS community loves to hate classes.
  • u

    user

    06/28/2019, 4:47 PM
    Copy code
    js
    class Layout extends Component {
      state = { isSidebarExpanded: true };
    
      handleRouteChange = (url) => this.setState({ isSidebarExpanded: true });
    
      componentDidMount() {
        Router.events.on('routeChangeComplete', handleRouteChange);
        this.beforeUnmounting(() -> Router.events.off('routeChangeComplete', this.handleRouteChange));
      }
      // ...
  • u

    user

    06/28/2019, 4:47 PM
    well, that's kind of the problem ^^
  • u

    user

    06/28/2019, 4:48 PM
    the thing that really revolts me about hooks is that they're inherently impure
  • u

    user

    06/28/2019, 4:48 PM
    Yep I dislike that too
  • u

    user

    06/28/2019, 4:51 PM
    I like your
    beforeUnmounting
    BTW.
  • u

    user

    06/28/2019, 4:51 PM
    thanks ;)
  • u

    user

    06/28/2019, 4:52 PM
    actually, it works best with tink signals, which return a cancellation token ... so you can go straight
    untilUnmounted(Router.routeChanged.handle(handleRouteChange))
  • u

    user

    06/28/2019, 4:52 PM
    I think a big source of mess is all those
    componentWillMount
    ,
    componentWillReceiveProps
    ,
    componentDidUpdate
  • u

    user

    06/28/2019, 4:53 PM
    well, all but the 3rd were killed
  • u

    user

    06/28/2019, 4:53 PM
    and the fact you can't call
    setState
    in the constructor so you can't reuse the same initialisation logoc
  • u

    user

    06/28/2019, 4:54 PM
    what happens when you call setState? ^^
  • u

    user

    06/28/2019, 4:54 PM
    you get an exception I believe
  • u

    user

    06/28/2019, 4:54 PM
    nice
  • u

    user

    06/28/2019, 4:55 PM
    so they handle the case where you make your own constructor and forget to pass props to super, but when you call setState in there, they hit you with the stick ... well played :D
  • u

    user

    06/28/2019, 4:56 PM
    Well in the constructor you may want to use your props to set the initial state, then when the props change... you need some other logic to change your state.
  • u

    user

    06/28/2019, 4:56 PM
    This is all really broken from the start
  • u

    user

    06/28/2019, 4:56 PM
    Other issues are around tracking that your component is still mounted when async operations complete.
1...111213...80Latest