score:3
As you have specified in your code that your class name is used using a state variable named 'class' containing the value 'tabBody tab activeTab'
className: this.state.class,
Thats why you must have to use setState() method to change your className. As you have the reference to your class instance in global namespace named 'Interface.tabBody' which can be used to set your className by calling setState() e.g
Interface.tabBody.setState({class: 'tabBody tab activeTab disabled'});
This method is used when you want to access class instance outside of the React.
if you are using handleclick() in your react then you can use the following code
handleTabClick = function() {
this.setState({class: 'tabBody tab activeTab disabled'});
}
By calling setState() the React will detect changes and re-renders component.
score:5
That's because this
is the reference to your class instance, not a DOM element. To access DOM elements (since React uses a virtual DOM) you need to create a reference, i.e:
React.createElement('div', {
ref: 'tabBody'
You can then access it via this.refs.tabBody
You shouldn't pass this reference outside of the class however. Instead you can pass the reference to handleTabClick
when the event happens:
React.createElement('div', {
ref: 'tabBody'
onClick: e => this.props.handleTabClick(e, this.refs.tabBody)
Then you can do:
handleTabClick = function(e, tabBody) {
tabBody.classList.add('tabPreviewComplete');
}
Personally I would change the state though, so that if the component re-renders it has the correct class.
score:8
Above 16.8, using useRef hooks for functional components,
// useRef takes initialValue as param
const fooBarRef = useRef(null);
//DOM Usage
<div className="foo" ref={fooBarRef}>Hello</div>
Usage of reference for useRef after mounting node element,
//Getting node element
const fooBarNode = fooBarRef.current
//Adding class to node element
fooBarNode.classList.add('bar');
Above 16.3, using createRef for class components,
// Creating ref in a constructor
this.fooBarRef = React.createRef();
// DOM usage
<div className="foo" ref={this.fooBarRef}>Hello</div>
Usage of reference for createRef after mounting node element,
//Getting node element
const fooBarNode = this.fooBarRef.current
//Adding class to node element
fooBarNode.classList.add('bar');
Below 16.3, using callBackRef,
// Creating ref in a constructor
this.fooBarRef = null;
this.setFooBarRef = (ref) => {
this.fooBarRef = ref;
}
// DOM usage
<div className="foo" ref={this.setFooBarRef}>Hello</div>
Usage of reference after mounting node element,
//Adding class name
this.fooBarRef.classList.add('bar');
Source: stackoverflow.com
Related Query
- How to get a React Component reference to change its class using classList?
- How does this functional component using react hooks get its parameters?
- How do you get a fade-in transition while using React useState hook to change class instead of CSS className:hover?
- How to make a react class component retain its state after login and change available routes based on the state currently available in the component?
- How do I get an attribute of an element nested in a React component using Jest and/or Enzyme?
- How to declare defaultProps on a React component class using TypeScript?
- How to notify parent component of property change when using react hooks?
- how to change image src using props with styled component and react
- How to test if React component is returning null or its children using React Testing Library?
- React How to get Component to detect route change
- How do I create css class in createMuiTheme() and use them directly without having to get its value from component "classes" props?
- I can not get the state from react router Link component using useLocation. So how can I pass it?
- React - How to get height of the particular div if you know its class
- How to get a reference to react component from a document event?
- How to pass function from FUNCTIONAL to CLASS component and access it outside of render( without prop ), using context in react js?
- How do you change the color of a React component using props
- How can I convert a class with a constructor to a functional component using React Hooks?
- How to get data from table row click using Semantic's React Table Component
- How to get my withStyles classes to work in my export class component in my React app
- How do I create a class decorator for mixing new methods into a React component when using Typescript?
- How can I add or remove class using querySelectorAll() in react functional component
- How to access data from get request made by a React functional component to localhost server using Axios?
- how to change the react component css class value
- How to change the state of the particular component when it is clicked, using useState in react js?
- How to reference and load Javascript file(s) inside a React Component using a React Hook
- How to get the data from Redux store using class components in React
- React SSR : How can my component get the URL search URL parameters in its render method
- How can I test if child React component was rendered based on a URL change using Jest and Enzyme
- How can I get a component to update on path change while using the same route and component?
- How do I reuse a React Component without having to change its inner props?
More Query from same tag
- ReactDOM Hydrate Renders HTML as Text on Client Page
- How to memoize a value based on a property of an object in React?
- Undefined while passing value of patientId
- How can I add a search function the data below (Ideally in another component)
- useEffect empty array callback does not work
- How to rename the header.actions field in Material-table?
- propTypes should be a func console warning when passing in array of icons
- React : Action creator not calling reducer
- percentage height to child inside of parent element with view port height does not work
- Show yup field validation when other fields are empty. At least one field has to be valid
- Form fields shrinking too small on mobile
- Convert React function component to a React Class Component
- Spread props based on conditional check
- How to obtain Open Weather API date/time from city being fetched?
- React: How to extract values from Modals?
- What is the reverse assignment (destructuring assignment) in ES6 javascript?
- My react input type == number is not working
- react-use-cart component. How to calculate the total price
- How to solve 'TypeError: Network request failed' in React Native?
- Looking for assistance in getting proper results from .filter()
- Does React suspense have other use cases apart from code splitting?
- Connection refused on localhost
- How to render a single data from an api call in reactjs
- Implement multiple interfaces in graphql
- Updating a component that is not mounted, according to console
- Sorting products by name ASC/DESC, price ASC/DESC in react
- How to fix the error argument of type string or undefined is not assignable to parameter of type string using typescript and react?
- onClick method in ReactJS
- Problem with writing custom editor for React-Data-Grid
- highcharts-react-official, how to implement setOptions from the official documentation, in react?