question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow mixins to export a view and remove render event.

See original GitHub issue

Mixins can extend your app’s state, actions and events. Take it one step further and let them export a view function too.

A mixin’s view would work by returning an object of one or more components.

// mixin
view(state, actions) {
  return {
    MyComponent,
    MyOtherComponent
  }
}

While the state, actions and events props are plain objects that we can merge for you, we can’t merge views as they are functions! But we can collect the views into an object and give it to you.

// app
view(state, actions, { MyComponent }) {
  return <MyComponent />
}

This can be useful for components that work together with a mixin like the Router/Link.

This means instead of e.g., myLibrary.Hello component and myLibrary.hello mixin, now you could do this instead.

export default function() {
  return {
    state: {
      hello: {
        color: "red"
      }
    },
    actions: {
      hello: {
        doSomething() { /* ... */ }
      }
    },
    view(state, actions) {
      return {
        hello: {
          Hello(props, children) {

            // This is a regular component.  
            // What's special about it is that's inside the view closure, so
            // it's effectively "pre-wired" to the state and actions.

            return (
              <a
                style={{ color: state.hello.color }}
                href={props.url}
                onclick={actions.hello.doSomething}
              >
                {children}
              </a>
            )
          }
        }
      }
    }
  }
}

Then you can then use it like this.

import hello from "hyperapp-hello"

app({
  view: (state, actions, { hello: { Hello } }) => (
    <div class="myapp">
      <Hello url="https://hyperapp.js.org" />
    </div>
  ),
  mixins: [hello()]
})

Your comments and questions please. 👋

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:14 (12 by maintainers)

github_iconTop GitHub Comments

1reaction
rajaraodvcommented, Sep 21, 2017
1reaction
andyrjcommented, Sep 21, 2017

@JorgeBucaran oh ya I can adapt my router, I’m just asking mainly out of curiosity if there are other use-cases where it couldn’t be replicated with the view exported from mixin. I can’t think of a use myself outside of routing.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Mixin for destroyed Vue component is still listening for events
In the parent component when I toggle the rendering by changing the isSearchingInFolders variable the expected component is destroyed and ...
Read more >
Developing Widgets | vtk.js - Kitware, Inc.
Removing a widget from a view is done by widgetManager. ... This allows to render complex widgets by using multiple simple and reusable...
Read more >
Higher-Order Components - React
A higher-order component (HOC) is an advanced technique in React for reusing component logic. HOCs are not part of the React API, per...
Read more >
Using mixins with class-based views - Django documentation
Let's look at how two of Django's generic class-based views are built out of mixins providing discrete functionality. We'll consider DetailView , which ......
Read more >
Mixins - Vue.js
Mixins are a flexible way to distribute reusable functionalities for Vue components. A mixin object can contain any component options. When a component...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found