Вы можете установить его в componentDidMount()
вроде этого:
export default class Roulette extends React.Component {
constructor(props) {
super(props);
this.socket = socket;
this.roulette = React.createRef();
this.state = {
result: null,
backgroundPositionX: null
};
}
componentDidMount() {
this.setState({
backgroundPositionX: (this.roulette.current.offsetWidth - 78) / 2
});
this.socket.on("roulette.roll", position => {
this.setState({
backgroundPositionX: position
});
//TODO: Retrieve from the server how much pixels should be translated
});
}
render() {
return (
<div className="main-r flex items-center justify-center">
<div ref={this.roulette} className="roulette relative">
<div
style={{backgroundPosition: this.state.backgroundPositionX + "px"}
className="roulette-bg h-full w-full" //The roulette background image, that is suposed to translate when the result is give is in this div
></div>
<div className="roulette-marker absolute"></div>
</div>
</div>
);
}
}
или вы можете использовать js, чтобы установить стиль элемента на странице, используя функциональность ElementCSSInlineStyle
. Это обсуждается здесь .
Я полагаю, вы бы поместили что-то подобное в ваш componentDidMount()
(используя перевод, как упомянуто @ yuri-gor):
const bgElement = window.document.querySelector('.roulette-bg');
bgElement.style.transform = 'translateX(42px)';