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.

destructuring-assignment should not trigger when a dynamic property name is accessed

See original GitHub issue

Example:

    getChildContext() {
        return {getProp: (prop) => this.props[prop]};
    }

There’s no way to change this to use destructuring, so the rule should not apply unless it’s this.props.foo or this.props['foo'].

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:8 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
CristianoYLcommented, Aug 8, 2018

Hi guys, I have a similar question. I’m quite new to React, so please correct me if I err.

I’m using React and Redux, and Redux allows me to expose the action creator functions as props inside my React component (using the bindActionCreators() function). I’ve attached my relevant code below, it’s a bit verbose.

The problem is that I’m fine with the destructuring pattern, but since queryVariants is already imported, I shall not declare another prop using this name. Any suggestions? Thank you!

import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';
import { queryVariants } from '../../actions';

// class-based component
class SearchBar extends Component {

  render() {
    return (
      <div>
           <button 
              onClick={() => { this.props.selectVariant(someData); }} 
           />
      </div>
    );
  }
}

// this function allows queryVariant function show up as a prop
function mapDispatchToProps(dispatch) {
  return bindActionCreators({ queryVariants }, dispatch);
}

export default withStyles(styles)(connect(null, mapDispatchToProps)(SearchBar));

[UPDATE] I removed some of the irrelavent code just to show the high-level structure of the component. It may be psuedo-code. The error comes from this line: onClick={() => { this.props.selectVariant(someData); }} where selectVariant() is exposed as props by redux.

1reaction
ljharbcommented, Mar 28, 2018
const { [prop]: foo } = this.props;

But you shouldn’t be dynamically reaching into props; exposing that kind of unfettered prop access to children totally violates the data model of React.

Read more comments on GitHub >

github_iconTop Results From Across the Web

destructuring-assignment should not trigger when a dynamic ...
I think this is a pretty valid case for children accessing parent props. All reactions.
Read more >
Dynamic property destructuring doesn't work for numbers
I'm trying to remove a dynamic property from myObject (in a react project). Here is myObject: myObject = { 'property1': {}, 'property2': {} ......
Read more >
Destructuring assignment - JavaScript - MDN Web Docs
Destructuring can be used with property names that are not valid JavaScript identifiers by providing an alternative identifier that is valid.
Read more >
Destructuring assignment JavaScript | End Your If
Dynamic Name Property ... Sometimes the property is not known while destructing something. Take a look! Now that we have user objects: Now...
Read more >
Destructuring of Props in ReactJS - GeeksforGeeks
A variable name that is not a copy of the property being destructured may be used. This is achieved by reassigning as shown...
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