Comments (23)
It depends on case, in a lot of cases where I need refs I don't use recompose at all and use just React classes, as refs are mostly used at low level.
Sometimes I use them like,
compose(
withHandlers(() => {
let ref_;
return {
registerChild: () => (ref) => {
ref_ = ref;
},
otherHandler: () => () => {
// I can use ref here.
}
};
})
)
Sometimes as like as above comment,
and sometimes for React Sortable for example I used it like
compose(
bc => SortableContainer(bc, { withRef: true }), // eslint-disable-line
toClass
);
so toClass
will allow to get Dom element instance of any component
from recompose.
Possibly but I would like to see some examples of how that is done.
Looking at the API reference for toClass() doesn't give me any ideas about how I can create a ref.
Could someone add an example or point me to a test?
from recompose.
I put together a jsfiddle that tests the output of the various HOCs here: https://jsfiddle.net/emilong/rcdovojw/3/
If this makes sense, I can take the output and add it to the documentation. One major assumption that would be good to get confirmation on is that Recompose.isClassComponent returning false means stateless component. Not sure if I'm getting the terminology and/or concepts right there.
from recompose.
Has anyone figured out how to actually use toClass
, especially with refs? Documentation seems to be missing here.
from recompose.
Thanks for the clarification. I'm thinking that recompose is not an ideal choice for this particular situation, as you mentioned earlier.
from recompose.
This is a problem with any function component, not just the ones provided by Recompose. One fix is add pure()
in between connect()
and withProps()
.
We should probably also document which helpers return a class component and which ones return a stateless component.
from recompose.
I found another solution: fresh version of redux
doesn't use refs
by default.
from recompose.
Should I close the issue?
from recompose.
I'll keep it open as a reminder to add documentation about refs.
from recompose.
Having a nice way to add refs with recompose becomes even more important as React deprecates returning a component from render. Normally our tests use this component to find the DOM objects, but in the future we are supposed to use refs.
So we need a great way to get a ref to the hoc via recompose so we don't have to resort to manually created classes. I was thinking maybe a new helper like withRef
? Something that will ensure that regardless of what you have composed together with recompose that you will get a valid ref.
from recompose.
@jeffbski does toClass()
work for your case?
from recompose.
Normally our tests use this component to find the DOM objects, but in the future we are supposed to use refs.
Why not use Enzyme? Refs are gross.
from recompose.
See this issue for an explanation of the current problems with HOCs and refs facebook/react#6974
from recompose.
I haven't seen a need for Enzyme yet, my tests are really simple without it, though now this may be a reason to use it.
I don't like refs either, but they are the recommended approach for getting the root component back in React 16+ facebook/react#6397 since they are deprecating the return value of render().
Frankly if it were up to me, I'd rather have a way to optionally provide a callback to render and just get async notified with the component rather than using a ref.
from recompose.
What is your use case for refs
with toClass
?
I'm asking because you need toClass
only if your component is a function, so why do you need an instance to it? If you need an instance of Dom element just write ref={registerRef} at top level Dom element of component.
from recompose.
If have contributed a ref function, but this.refs
is always empty when I attempt to make use of this ref.
from recompose.
For example:
const enhance = compose(
lifecycle({
componentDidUpdate: function(prevProps) {
console.log(this._myRef) // undefined
}
})
)
class Foo extends React.Component {
render() {
return <div ref={ component => this._myRef = component } />
}
}
export default enhance(Foo)
from recompose.
Why not to register refs explicitly using function syntax in your case? ie
const MyComp = ({ registerChild }) => <div ref={registerChild}>Blabla</div>
Now you can get ref of MyComp
using <MyComp registerChild={registerChild} />
from recompose.
Your case with lifecycle will not work at all, all is enhancer
not the same component.
Your this
points to lifecycle
component instance not on yours. Please read about Higher-Order Components
from recompose.
so how can I get access to an element in this case? stateHandler as my ref function? or something else?
from recompose.
I don't know your case, I wrote before one of example how. There are many ways to do this.
from recompose.
i don't know what registerChild
is in your example
from recompose.
What it can be based on React documentation?
from recompose.
Related Issues (20)
- renderNothing should export Nothing HOT 4
- Does TypeScript 3.4 enable typing of compose HOT 1
- Remove flow types in repo and support only in flow-typed repo HOT 1
- Key advantages in Docs HOT 2
- The best way to test a component that use getContext recompose HOT 1
- Get a lifecycle componentDidMount unmounted error HOT 1
- Rewriting mapPropsStream with Hooks or new Lifecycle Methods HOT 4
- compose hoc causes to lose typings
- Remove usage of `createFactory` HOT 4
- Why does `mapProps` remove `children`? HOT 2
- node-fetch vulnerability issue (denial of service) HOT 8
- Forked, Updated, and Published HOT 1
- fbjs dependency still present in lock file HOT 3
- Support React 17 HOT 2
- Suggest alternative to branch(), renderComponent(), renderNothing(), compose(), etc
- TypeError: Cannot read properties of undefined (reading 'apply')
- Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports.
- New patch required to mitigate high risk vulnerability
- Maintainers HOT 1
- This is GITHUB and not the European Parliament or NATO.
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from recompose.