score:2
Your issue is happening in your HOC:
here
const withCustomPagination = tableRef => Component => {
You need to remove that parameter. The way to access to the ref prop is simply in your componentDidUpdate
method like forwardedRef
prop e.g:
import CustomPagination from 'path/to/file';
const withCustomPagination = Component => {
class WithCustomPagination extends React.Component {
constructor(props) {
super(props);
this.state = {
rows: 1,
dataLength: props.dataLength,
}
}
componentDidUpdate() {
//You got the ref here
console.log(forwardedRef.current)
}
render() {
const { forwardedRef } = this.props;
return (
<Component {...this.state} ref={forwardedRef} />
<CustomPagination />
)
}
}
return React.forwardRef((props, ref) => {
return <WithCustomPagination {...props} forwardedRef={ref} />;
});
}
export default withCustomPagination;
Also somethings to have in account are:
You should not create the ref in the render method because this method is raised every time you set a state. I recommend you to do it in the constructor:
import WrappedTable from '/path/to/file';
class App extends React.Component {
constructor(){
super();
this.reference = React.createRef();
}
render() {
return (
<WrappedTable ref={this.reference} />
)
}
}
Also in you HOC render only one child or use React.Fragment
. Besides do not forget the send the rest properties:
const withCustomPagination = Component => {
class WithCustomPagination extends React.Component {
constructor(props) {
super(props);
this.state = {
rows: 1,
dataLength: props.dataLength,
}
}
componentDidUpdate() {
//You got the ref here
console.log(forwardedRef.current)
}
render() {
// Do not forget to send the rest of properties here like:
const { forwardedRef, ...rest } = this.props;
return (
<React.Fragment>
<Component {...this.state} ref={forwardedRef} {...rest} />
<CustomPagination />
</React.Fragment>
)
}
}
return React.forwardRef((props, ref) => {
return <WithCustomPagination {...props} forwardedRef={ref} />;
});
}
export default withCustomPagination;
EDIT:
Add the reference of the ref prop
import withCustomPagination from '/path/to/file';
class Table extends React.Component {
constructor(props) {
super(props);
this.reference = React.createRef();
}
render() {
<TableContainer ref={this.reference} />
}
}
const WrappedTable = withCustomPagination(Table);
export default WrappedTable;
score:0
To access the tableRef
in HOC withCustomPagination
, I removed const tableRef = React.createRef()
from App.js and the corresponding ref = {tableRef}
lines.
I pass tableRef
to HOC, curried, withCustomPagination(tableRef)(NewGiftCardTable)
. I also removed all the Forwarding Refs logic in HOC, this I did because I needed access to tableRef
only in HOC and not in App.js.
Added above removed lines to Table.js:
import withCustomPagination from '/path/to/file';
const tableRef = React.createRef();
class Table extends React.Component {
constructor(props) {
super(props);
}
render() {
<TableContainer ref={tableRef} />
}
const WrappedTable = withCustomPagination(tableRef)(Table);
export default WrappedTable;
Source: stackoverflow.com
Related Query
- Forwarding Refs: forwardedRef is always null
- Refs are always null inside modal component
- React - Forwarding multiple refs
- current is always null when using React.createRef
- Refs are null in Jest snapshot tests with react-test-renderer
- React.createRef always null
- AsyncStorage.setItem Callback Always Null
- InputEvent.dataTransfer is always null
- Forwarding refs by multiple levels [Typescript / React]
- google.maps.places.PlacesService(map) always returns null
- Forwarding refs through multiple components
- React.addons.TestUtils.renderIntoDocument always returns null
- Appended JavaScript object to form data is always received as null on server
- React Refs return null or undefined when accessing reference to the DOM node
- React "this" is always null in functions
- Prop always null in react
- .Net Core React + Web Api form post data always null
- why the createRef current always null in react
- Why do functional components wrapped in forwardRef receive null refs when I use the useContext hook?
- Using React.createRef in class component has current as always null
- Why is state is always null in my redux application?
- How to use Forwarding Refs with react-router-dom
- want to get html component as image but always document.querySelector return null
- React forwardref current is always null
- Ref current is always null when rendering my element for the first time?
- NextJS next-auth get session always null
- React lodash debounce always return null
- React: send keyed children to a component. Keys always null
- Firebase Cloud Functions called from app context is always null
- Nextjs graphql apollo cache is always returning null
More Query from same tag
- Hide header on Login page
- TypeError: Cannot read properties of undefined (reading 'value') for a input within a form
- Redux: Call to action-creator returns 'Cannot read property 'props' of undefined'
- Emit data from stencil component to ReactJs component
- Call JS function on script tag
- ReferenceError: Cannot access 'search' before initialization
- React / Redux - setting initial state with useRouteMatch
- navbar re rendered when state changed
- Disable button till all the form fields get filled in Formik
- How to make the email input stay on the same page so that user does not have to enter the email again?
- Error: Uncaught TypeError: Cannot read property 'map' of undefined
- Is there a way to write a custom react hook that can injected into a component?
- reactjs Redux, how to load data from redux onComponentDidmount while a first action is already sent
- React Router `history` and `location` mismatched after back button
- React file throws error Cannot read property 'style' of undefined only when I refresh the page
- Putting two functions in the same "onclick"
- React/JS toggle table columns
- How to set var or function call from action to component
- Add a tooltip only on disabled dropdown item from a dropdownButton (react-bootstrap)
- How do I refactor this React component with a listener, to make the component functional?
- Update object based on id, error 'this.props.update' is not a function
- I cannot change the array in State
- How to fix 'TypeError: Cannot read property 'style' of undefined' error when using material-ui Transitions
- Why am i getting a unique key prop warning when i have a unique key?
- Module not found Can't resolve react-scroll-to-bottom
- cancelling out possibility of repeat display using ternary operator
- Remove comma if its the last item in the object
- If React props are readonly, how they can change (whats the usage of getDerivedStateFromProps)?
- React Native image upload
- In React ES6, is it safe to change an HTML element's tag by creating a React element from a property turned string?