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.

add `options` property to the render context of functional component

See original GitHub issue

What problem does this feature solve?

Custom properties in the options of functional component can’t be accessed easily.

What does the proposed API look like?

In the render function of functional component, options can be accessed by context.options, just like vm.$options(https://vuejs.org/v2/api/index.html#vm-options)

related issue #7492

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:8
  • Comments:13 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
Aaron-Poolcommented, Apr 25, 2018

Sure! Disclaimer though, I’m fairly new to vue. I have pretty extensive experience in other front end frameworks, but I’m new to adopting vue. There’s a very real chance I’m doing something ridiculous and unintuitive, but the use case doesn’t strike me that way, personally.

Suppose I have a mixin that takes a custom property “types” from a component definition, assuming its present, and checks for matching attributes on the component’s host element. It then concatenates a string of styles, derived from the attributes specified on the component element that match a type specified in the component definition. Here’s how I might do it (I’m using styled components. Hopefully you’re familiar with the library):

So, suppose this is my component:

import styled                 from 'vue-styled-components';
import IsTyped                from '@Composables/IsTyped';
import { Typography, Colors } from '@Constants/style';
let SmallLabel = {
 functional: true,
  name : 'SmallLabel,
  mixins : [IsTyped],
  render : function(h, context) {
    let Label = styled.span`
        // define base styles
        font-size   : ${Typography.size.medium};
        color       : ${Colors.black};
        // now add any styles based on types provided in the attributes
        ${context.$options.typedStyles} // this will be set by my mixin
      `;
    return (<Label>{context.$slots.default}</Label>);
  },

  types : {
    bright : `color      : ${Colors.smoke};`,
    dark   : `color      : ${Colors.sepia};`,
    bold   : `font-weight: ${Typography.weight.bold};`,
    light  : `font-weight: ${Typography.weight.bold};`
  }
};

And here’s my IsTyped mixin:

import {keys, intersection, values} from 'lodash';

export default {
    beforeMount: function() {
      let styles = '';
      // looks for intersection between a components attribute and specified types
      intersection(keys(this.$options.types), values(this.$attrs))
        .forEach((t) => {styles = styles.concat(this.types[t]);})
      // concatenates the styles and then attaches them to the custom options of the component
      this.$options.typedStyles = styles;
    }
  }
}

Unfortunately, this does not work. Because $options is not on the context object provided to the render function.

1reaction
caikancommented, Apr 23, 2018

My initial idea was to let functional components can be extended dynamically. Extended components have the same render logic but different options. Now I think I have found another workaround: using a factory function.

function createResponsiveComponent(options) {
  return {
    functional: true,
    render(h, context) {
      let component = options[getDeviceType()];
      return h(component, context.data);
    },
  };
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Components and Props - React
Rendering a Component · We call root.render() with the <Welcome name="Sara" /> element. · React calls the Welcome component with {name: 'Sara'} as...
Read more >
How to use React Context like a pro - Devtrium
Use React Context with a custom Provider. First, we'll create a UserContextProvider component inside of a new file called UserContext.jsx . This ...
Read more >
Render Functions & JSX | Vue.js
To create a functional component we use a plain function, rather than an options object. The function is effectively the render function for...
Read more >
Functional Components in Vue.js - DigitalOcean
The context argument you see on the render function is known as the render context. It's an object containing the following properties: props ......
Read more >
Using React Context with Functional Components | by Dan Fyfe
Pretty simple. All we have done is made an InfoContainer component that contains a ListContainer and a GraphContainer. We then wrapped both of...
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