According to https://facebook.github.io/react/tips/inline-styles.html
CSS styles need to be passes as an object to the component. However, if you are trying to use transform styles you will get an error.
To change the style of an element on click in React:Set the onClick prop on the element. When the element is clicked, set the active state. Use a ternary operator to conditionally set the new styles based on the state variable.
The translate CSS property allows you to transfer an element from one place to another along the X (horizontal) axis, the Y (vertical) axis, and the Z (depth) axis, similar to how you might think of moving an element using offsets, like top , bottom , left , and right .
Translate also wasn't working for me. I fixed it by adding 'px' behind the var.
ES6 code:
render() { const x = 100; const y = 100; const styles = { transform: `translate(${x}px, ${y}px)` }; return ( <div style={styles}></div> ); }
My solution was to first concatenate the string and then pass it to the object. Notice the use of 'px' here.
render: function() { var cleft = 100; var ctop = 100; var ctrans = 'translate('+cleft+'px, '+ctop+'px)'; var css = { transform: ctrans } return ( <div style={css} /> ) }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With