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 getter hooks to reuse the previous props

See original GitHub issue

I’m digging into the getter hooks, like the getCellProps etc. — what I’m wondering is if it is somehow possible right now to access the previous values of the props, in order to manually merge/reuse them?

Example: I want to access the current colSpan, and add some styles based on it or modify it. But from what I’ve seen, this is not possible from the hooks. If I’m not missing something, then I can see this could be added in one of the following ways:

  1. Getter hooks could have the third argument after the Cell and TableInstance, which would contain the currently accumulated props.

  2. Alternatively, the currently accumulated props could be probably stored inside a Cell?

Currently, I can see that the only way of doing what I want is outside of a plugin-hook, by using the cell.getCellProps() during the Table generation and passing its values to my other function. But I feel that having a way to access the props from the previous hooks could be very powerful and could allow to create quite flexible plugin-hooks 😃

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:13 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
tannerlinsleycommented, Dec 10, 2019

The most important parts are how the prop getters work:

// Auto extend with smart-merging styles and classNames
hooks.getThingProps.push({
  newProp: true,
})

// Multi auto extend with smart-merging styles and classNames
hooks.getThingProps.push([{
  style: {...},
  className: 'one'
}, {
  style: {...}, 
  className: 'two'
}])

// Functional replacement with both options above:
hooks.getThingProps.push((props, instance, row) => [
  props,
  {
    style: {...}, 
    className: 'two'
  }
])

// This will ditch old props
hooks.getThingProps.push((props, instance, row) => [
  // props,
  {
    style: {...}, 
    className: 'two'
  }
])

// This will do defaults
hooks.getThingProps.push((props, instance, row) => [
  {
    style: {...}, 
    className: 'two'
  }
  props,
])

// This will replace all props without smart merging
hooks.getThingProps.push((props, instance, row) => [
  {
    ...props,
    style: {...}, 
    className: 'two'
  }
])

More importantly, all of these options are also supported via the actual resulting prop getter function on the instance. So you should never need to use mergeProps either (which is also why it’s deprecated now 😉)

1reaction
tannerlinsleycommented, Dec 10, 2019

I have a pretty decent solution that will fit all of these use cases. I’m currently working up a PR. Hang tight.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Allow getter hooks to reuse the previous props #1749 - GitHub
I'm digging into the getter hooks, like the getCellProps etc. — what I'm wondering is if it is somehow possible right now to...
Read more >
Accessing previous props or state with React Hooks
Leverage the useRef, useState, usePrevious, and useEffect React Hooks to access previous props and states from within functional components.
Read more >
What is the Prop Getters Pattern? - Educative.io
Let's start by refactoring the previous solution to use a props getter.
Read more >
Using state setter as prop with react hooks - Stack Overflow
A setter of state is just a function value from prop's view. And the call time can be anytime as long as the...
Read more >
React Hooks: What's going to happen to render props?
What am I going to do with all these render props components now that react hooks solve the code reuse problem better than...
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