GithubHelp home page GithubHelp logo

Can't make `branch` work about recompose HOT 7 CLOSED

acdlite avatar acdlite commented on July 20, 2024
Can't make `branch` work

from recompose.

Comments (7)

antoinerey avatar antoinerey commented on July 20, 2024

I found the solution:

In order to make it work, I had to specify a BaseComponent as last argument of the branch function. Nevertheless, I still don't get why the exemple from the doc works. (I assume that it works).

Does anyone have an explanation ? :)

from recompose.

wuct avatar wuct commented on July 20, 2024

Because branch returns a function which accepts four arguments. Without providing BaseComponent, what you get is just a function not a React component.

You can use lodash/identity, which is a function arg => arg, to achieve what you expect in a more semantic way.

export default branch(
  props => !!props.title,
  identity,
  renderComponent(Loader)
)(Page)

from recompose.

antoinerey avatar antoinerey commented on July 20, 2024

How does that work in this exemple then ? The branch function is not called with 4 arguments.

from recompose.

mrblueblue avatar mrblueblue commented on July 20, 2024

Just jumping in here: in the end branch is called with the four arguments -- spinnerWhileLoading seems to be a curried branch function. (Please correct if I misunderstood something!)

from recompose.

wuct avatar wuct commented on July 20, 2024

@mrblueblue you are correct.

In the example:

const Post = spinnerWhileLoading(

  // Here is the fourth argument
  props => props.title && props.author && props.content,

  // Here is a functional component 
  ({ title, author, content }) => (                                           
    <article>
      <h1>{title}</h1>
      <h2>By {author.name}</h2>
      <div>{content}</div>
    </article>
  )
);

from recompose.

StevenLangbroek avatar StevenLangbroek commented on July 20, 2024

Yeah I'm having issues getting this to work in compose flow as well:

// This works, but I have to repeat the component to render after loading:
const preload = (test, Component) => branch(
  test,
  renderComponent(Loader),
  renderComponent(Component)
);

export default compose(
  provideHooks(mapHooks),
  reduxForm(mapFormToProps, mapStateToProps, mapDispatchToProps),
  preload(
    (props) => props.isLoading,
    MyComponent
  )
)(MyComponent);

// This doesn't work, moved the Component 1 level down so we get it from `compose`. 
// The weird thing is, I do see the Component class (MyComponent in this case) 
// coming down correctly if I log out the args for the returned function...

const preload = (test) => (Component) => branch(
  test,
  renderComponent(Loader),
  renderComponent(Component)
);

export default compose(
  provideHooks(mapHooks),
  reduxForm(mapFormToProps, mapStateToProps, mapDispatchToProps),
  preload(
    (props) => props.isLoading,
  )
)(MyComponent);

/**
Results in this error:
Invariant Violation: Component(...): A valid React element (or null) must be returned. 
You may have returned undefined, an array or some other invalid object.
*/

What's going on here? Am I missing something?

Update: so I got it working, but this code is very counterintuitive for me. Is this the way to do this?

const preload = (test) => (Component) => branch(
  test,
  renderComponent(Loader),
  renderComponent(Component)
)(Component);

from recompose.

acdlite avatar acdlite commented on July 20, 2024

@StevenLangbroek You don't need the inner function. This will do:

const identity = t => t

const preload = (test) => branch(
  test,
  renderComponent(Loader),
  identity
);

Sounds like the confusion arose from two things:

  • It may be unclear to some that the 2nd and 3rd arguments to branch are higher-order components, not components.
  • Currying is confusing. We removed it in 0.17.0 for this reason.

Closing this issue, but if you have any other questions, let us know :)

from recompose.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.