score:0
The problem is, that you're trying to access the element before it appears in the DOM (try to console.log(document.body.innerHTML)
right under the React import and check what does the DOM look like at the time of calling the getElemenetById
function.
If you need the element's reference you should use ref.
score:2
You are trying to get a node that has not been rendered yet.
You could use the componentDidMount
lifecycle method by converting your component to a stateful (class component) one before trying to get your canvas. But keep in mind that this is considered really bad practice and should never be necessary :
componentDidMount(){
this.canvas = document.getElementById('dial__container');
}
Another solution would imply using refs
, as linked in the other answer, which are also considered a bad practice.
Source: stackoverflow.com
Related Query
- How to access DOM in functional component
- React, how to access the DOM element in my render function from the same component
- How to access props.children in a stateless functional component in react?
- How to access the latest state value in the functional component in React
- How to access redux's store state variable in a functional component using useSelector?
- How to pass function from FUNCTIONAL to CLASS component and access it outside of render( without prop ), using context in react js?
- How to access the DOM element of the child component in Preact with hooks?
- How to access file from functional component in react
- How to access data from get request made by a React functional component to localhost server using Axios?
- How to call a react hook fetch request in a functional component to access data then pass to a class component to map?
- How to access props in react functional component using react and typescript?
- How to access param in parent component with React Router DOM
- How to access state in a React functional component from a setTimeout or setInterval callback?
- How to access inner virtual DOM elements on onChange call from a parent component in React
- How do I access a ref in a functional child component that is wrapped in React-Redux's connect function?
- How to access values from context in a separate functional component
- React functional component - How to access children method from Parent
- How to access react-list getVisibleRange() within a functional component
- How do I access the Context and Component state from within a DOM callback in a function React component?
- How to access my redux state outside of the functional component or class component?
- How to access a value of the array present in useEffect, in the return function of the functional component in react.js?
- How to access a DOM element in React? What is the equilvalent of document.getElementById() in React
- How to access component methods from “outside” in ReactJS?
- How to use children with React Stateless Functional Component in TypeScript?
- How do I access refs of a child component in the parent component
- How to access the "key" property from a reactjs component
- how to set displayName in a functional component [React]
- How to access one component's state from another component
- How to extend styled component without passing props to underlying DOM element?
- ReactJS - How can I access the displayName of a component using javascript?
More Query from same tag
- React Add Item with spread operator
- why split app and vendor js code fails in webpack & react
- How to start search only when user stops typing?
- Add a tooltip only on disabled dropdown item from a dropdownButton (react-bootstrap)
- Run function on React.Intl <FormattedMessage>
- The ask of Date method in React
- Basic React: why is a variable in local storage resetting to undefined after briefly being the desired value?
- React router in TypeScript- both router and own props
- Redux state change but react will not Rerender
- Redux: Dynamically pass component to Provider
- Webpack-dev-server injects bundle in HTML page, but nothing is rendered
- Webpack image loader Error
- I want to change the state value of array while using Redux toolkit
- Responsive images with <picture> and <source> in ReactJS
- How add new object property while updating state in React
- How to not fire throttle function from lodash?
- property missing in returned json response / javascript
- What is the purpose of boolean values for the breakpoint props (xs, sm, md...) in material-ui
- React Redux Hooks not finding State
- How to implement authentication in Next.js
- Invalid value for createPaymentMethod: card should be object or element. You specified: null
- Nested fetch/then methods
- onClick event not working within nested React / Next.js component
- react.js antd carousel with arrows
- Tone.js - How do you make a LFO object the control input for a Volume Object that is part of an oscillator chain?
- Working with complicated immutable objects
- How is state being used in react
- Setting Selected State to Mapped Components
- The whole page scrolls to right when dragging events from the TreeView (schedule/external-drag-drop)
- Create an extensible architecture with React