score:3
You're missing a few things here
First off, you're writing an ES6 component class and when using ES6 together with React, React no longer features auto binding of this
making your call to handleCallback
refer to a this
which will be null.
this.handleCallback.bind(this)
or declare your callback with an arrow function handleCallback = () => {}
will take care of that problem.
And secondly, the way you're trying to set the className is not allowed, React only updates the tree (i.e., what you see) when re-rendering and React will never flush the changes you made 'inline' back into the DOM so you would have to utilize this.setState()
or this.forceUpdate()
in order to be able to see, but since your changes are inline they will never persist on a subsequent render.
Below is a very verbose Component to illustrate how to do it
class Button extends React.Component {
constructor() {
super();
this.state = {
shouldWiggle: false
};
}
handleCallback() {
this.setState({shouldWiggle: !this.state.shouldWiggle});
}
render() {
let cls = this.state.shouldWiggle ? 'icon-up wiggle': 'icon-up';
return (
<div>
<span className='highlight'>{this.props.summary}</span>
<a
className='link'
onClick={this.handleCallback.bind(this)}
>
<span ref={(c) => this.icon = c} className={cls}>ad</span>
</a>
</div>
);
}
}
Source: stackoverflow.com
Related Query
- ref does not work in this reactjs component
- this.node.contains does not work if this.node is a ref to react component
- ReactJS. - Why does this work in ReactJS and not the other way
- I have this child component where you can submit a form, however, e.preventDefault() does not work
- Reactjs form submit involving a controlled select component does not work
- Reactjs / Material-UI: className does not work on the ToggleButton component
- Reactjs - Material-UI component does not work in function
- Bootstrap classes do not work in ReactJS component
- onClick does not work for custom component
- Material UI component reference does not work
- Why does hot reloading not work for reactjs visual studio 2019 template
- React - component in seperate script does not work
- CSS Modules + Ant design in ReactJs does not work
- React input[type=date] component does not work properly
- React Admin: onSuccess does not work properly on <Edit> Component
- Why does custom Button component using MUI Button not work on hover with Tooltip?
- ReactJS - CSS: setting trasitionProperty in style object does not work
- ReactJS - Inline styling does not update upon rerendering of component
- How to attach a compound component when using React forward ref (property does not exist on forwardRefExoticComponent)
- TypeScript linting error: ref does not exist on component
- Draft.js (react-draft-wysiwyg): text update from the outside of the Editor component does not work
- Reactjs Link does not work on IE11 but does on Edge and other browsers
- ComponentDidMount does not work - props do not pass to child component
- .js component in a .tsx component - no overload matches this call/property does not exist on type IntrinsicAttributes
- Reactjs component does not call componentWillMount method when route is matched
- Why ES6 ComputedPropertyName does not work with this react js code?
- React component and Apollo Client does not work "in real time"
- Reactjs - Routing is not work on imported component
- FieldArray component does not work
- Does ReactJS not work with Zurb Foundation Accordions?
More Query from same tag
- How to pass the state of the page to other React?
- Div does not follow the mouse
- Uncaught Error: Cannot find module 'react/jsx-runtime'
- add new field in every json object in a array javascript
- Change component value
- Why does this method updates react state very slowly?
- React : can't update my state with setState
- Create array of arrays from object properties in ReactJS
- How to type inded signature in object
- Material-ui - TextField - Can't change helper text error color
- React multiple api requests
- how to fix bad request
- mapDispachToProps is giving error while firing an event onChange function
- re-render react component after i deleted from local storage?
- Unable to access this.props data inside constructor, react+redux
- run function from child component reactjs
- Is this an example of destructuring assignment?
- Gatsby - how to fix "eliminate render-blocking resources" (google fonts)
- Is it okay to call an async function that is fetching initial data in the constructor of a JS class?
- React. How to use the keys in setState with array indexes?
- Render N Tables - ReactJS, next table depends on previous table
- How do I configure a React router to load two components on top of each other (Overlay)?
- Property 'className' does not exist on type '{ props: ReactNode; }'
- Bootstrap navigation with dynamic heading
- asynchronous search promises not resolving in order
- React Bug In Displaying Element With The Same Key
- Is there a way to utilize React's `history.push()` to trigger a file download?
- Uncaught Error: A cross-origin error was thrown in React
- In React after clicking a button I want to change it immediately to the previous state?
- Action creators not showing in "this.props" with redux-form 6.0.0-rc.3