【发布时间】:2016-05-07 18:20:54
【问题描述】:
我正在做一个更大的项目,但我创建了这个简短的示例来说明问题。
如果我使用Box 组件,它可以工作。它在控制台中输出componentWillEnter 和
componentWillLeave 当我们点击按钮时。
如果我使用BoxContainer 容器,它就不再起作用了。不调用 componentWillEnter 和 componentWillLeave 特殊的生命周期钩子。
{this.state.shouldShowBox && <BoxContainer/>}
Webpack 构建已正确完成,控制台中没有错误,但它什么也没输出。
我还尝试使用高级 API :ReactCSSTransitionGroup,它适用于 Box 和 BoxContainer 组件。但我需要使用低级 API:ReactTransitionGroup。
您知道为什么我们使用react-redux 时它不起作用吗?
使用的版本:
- 反应:15.0.2
- redux : 3.5.2
- react-redux:4.4.5
代码:
import React from 'react';
import {render} from 'react-dom';
import {Provider, connect} from 'react-redux';
import {createStore, compose, combineReducers} from 'redux';
import TransitionGroup from 'react/lib/ReactTransitionGroup';
// Box component
class Box extends React.Component {
componentWillEnter(callback) {
console.log('componentWillEnter');
callback();
}
componentWillLeave(callback) {
console.log('componentWillLeave');
callback();
}
render() {
return <div className="box"/>;
}
}
// Boxe connected component
const BoxContainer = connect(null, {})(Box);
// App component
class App extends React.Component {
state = {
shouldShowBox: true
};
toggleBox = () => {
this.setState({
shouldShowBox: !this.state.shouldShowBox
});
};
render() {
return (
<div>
<TransitionGroup>
{this.state.shouldShowBox && <BoxContainer/>}
</TransitionGroup>
<button className="toggle-btn" onClick={this.toggleBox}>
toggle
</button>
</div>
);
}
}
// reducer
function reducer(state = [], action) {
switch (action.type) {
default:
return state
}
}
// store
const store = createStore(reducer);
// render
render(
<Provider store={store}>
<App />
</Provider>
,
document.getElementById('root')
);
【问题讨论】:
标签: javascript reactjs react-redux