なので、もう少し休んだ後、私は、「学生が学生を教える」という別の章があると決めた(指示され、主演してくれた)。
私は自分自身の質問にもっとsuccintly、transitionendイベントリスナーは、要素にリスナーを接続し、移行が終了するたびにコールバック関数を起動します。私が持っていた問題は、これが非同期であるため、移行が完了する前にDOMがレンダリングするので、componentWillUpdateに配置することはできませんでした。私の現在の回避策は、ボタンをクリックしてイベントリスナーを含む関数を呼び出し、trnasitionendのコールバック関数でコンポーネントの状態を変更させることです。この方法では、遷移が完了するまでレンダリングは行われません。
コード:
class Button2 extends React.Component {
constructor(props){
super(props)
this.onClickHandler = this.onClickHandler.bind(this)
}
onClickHandler(){
this.props.onClick("Button2")
}
render() {
return (
<button onClick={this.onClickHandler} id="button2">
BUTTON2!!
</button>
)
}
}
class Button1 extends React.Component {
constructor(props){
super(props)
this.onClickHandler = this.onClickHandler.bind(this)
}
onClickHandler(){
this.props.onClick('Button1')
}
render() {
return (
<button onClick={this.onClickHandler} id="button1">
BUTTON1!!
</button>
)
}
}
class App extends React.Component {
constructor(props){
super(props)
this.state = {
showButton : true,
hide: false
}
this.changeButtonState = this.changeButtonState.bind(this)
this.getButton = this.getButton.bind(this)
this.transitionEndEventName = this.transitionEndEventName.bind(this)
this.hideApp = this.hideApp.bind(this)
this.removeEvent = this.removeEvent.bind(this)
}
getButton() {
if (this.state.showButton){
return <Button1 onClick={this.hideApp}/>
} else {
return <Button2 onClick={this.hideApp}/>
}
}
transitionEndEventName() {
var el = document.createElement('div')//what the hack is bootstrap
var transEndEventNames = {
WebkitTransition : 'webkitTransitionEnd',
MozTransition : 'transitionend',
OTransition : 'oTransitionEnd otransitionend',
transition : 'transitionend'
}
for (var name in transEndEventNames) {
if (el.style[name] !== undefined) {
return transEndEventNames[name];
}
}
return false // explicit for ie8 ( ._.)
}
hideApp(button) {
var app = document.getElementById('main')
var transitionEnd = this.transitionEndEventName()
app.addEventListener(transitionEnd, this.removeEvent, false)
app.classList.contains('show-element') ? app.classList.remove('show-element') : null
app.classList.add('remove-element')
}
removeEvent(){
console.log('hey')
var app = document.getElementById('main')
var transitionEnd = this.transitionEndEventName()
app.removeEventListener(transitionEnd, this.removeEvent, false)
this.changeButtonState()
}
changeButtonState(button) {
this.setState({
showButton: !this.state.showButton,
hide: true
})
}
componentDidUpdate(){
var app = document.getElementById('main')
app.classList.contains('remove-element') ? app.classList.remove('remove-element') : null
app.classList.add('show-element')
}
render(){
var button = this.getButton()
return (
<div id="button-container">
{button}
</div>
)
}
}
ReactDOM.render(<App />, document.getElementById('main'))
codepen (誰もがこれを読んで、あなたはその適切にその途中でアプリ遷移アウトではなく、同様ときに気づくでしょうcodepen見ている場合codepen
をチェック誰かがこれをなぜ知っているのか知ってうれしいです。 –