我试图使用React-Redux。我有一种情况,其中一个连接组件( Coordinates.jsx )嵌套在另一个连接组件( Canvas.jsx )中。
源代码https://github.com/RinatRezyapov/Vault-13.git(纱线安装,然后纱线启动)
在父组件 Canvas.jsx 中,我在 componentDidMount()中调度一个不可改变状态的操作。
Canvas.jsx
componentDidMount() {
this.props.addCanvasSize(windowWidth, windowHeight)
}
问题是在更改状态后,子组件 Coordinates.jsx 没有更新,console.log显示未定义。
Coordinates.jsx
componentDidMount() {
console.log(this.props.canvasSize)
}
我确定状态正确更新(使用Redux devTool检查),我想我没有在减少状态下改变状态。
如果我在setTimeout中包装console.log(this.props.canvasSize),那么它会显示coorect状态。
index.js(商店)
const store = createStore(reducer,
window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__())
render(
<Provider store={store}>
<App />
</Provider>,
document.getElementById('root'));
操作/ index.js
export const addCanvasSizeAction = (windowWidth, windowHeight) => ({
type: 'ADD_CANVAS_SIZE',
windowWidth,
windowHeight
})
减速器/ reducer.js
export const reducer = (state={}, action) => {
switch (action.type) {
case 'ADD_CANVAS_SIZE':
return Object.assign({}, state, {canvasSize: {canvasWidth: action.windowWidth, canvasHeight: action.windowHeight}})
default:
return state
}
}
组件/ App.jsx
class App extends React.Component {
render() {
return (
<div>
<CanvasCont />
</div>
)
}
}
组件/ Canvas.jsx
class Canvas extends React.Component {
constructor(props) {
super(props);
}
componentDidMount() {
var windowWidth = window.innerWidth-100;
var windowHeight = window.innerHeight-100;
var createCanvas = document.createElement("canvas");
createCanvas.id = 'canvas';
createCanvas.width = windowWidth;
createCanvas.height = windowHeight;
ReactDOM.findDOMNode(this).appendChild(createCanvas);
var rect = canvas.getBoundingClientRect();
var ctx = createCanvas.getContext('2d');
this.props.addCanvasSize(windowWidth, windowHeight)
}
render() {
return (<div>
<CoordinatesCont />
</div>)
}
}
组件/ Coordinates.jsx
class Coordinates extends React.Component {
constructor(props) {
super(props);
}
componentDidMount() {
console.log(this.props.canvasSize)
}
render() {
var inlineStyle={
width: "800px",
height: "600px"
}
return (
<div >
<span style={inlineStyle} onMouseMove={this.handleMouseMove}></span>
</div>
)
}
}
容器/ CanvasCont.js
const mapStateToProps = (state) => ({
state: state
})
const mapDispatchToProps = (dispatch) => ({
addCanvasSize: (windowWidth, windowHeight) =>
{dispatch(addCanvasSizeAction(windowWidth, windowHeight))}
})
const CanvasCont = connect(
mapStateToProps,
mapDispatchToProps
)(Canvas)
容器/ CoordinatesCont.js
const mapStateToProps = (state) => ({
canvasSize: state.canvasSize
})
const mapDispatchToProps = (dispatch) => ({
})
const CoordinatesCont = connect(
mapStateToProps,
mapDispatchToProps
)(Coordinates)
答案 0 :(得分:2)
在处理父/子关系时,您应该记住,在孩子被调用之前,会调用componentDidMount
(如本答案https://stackoverflow.com/a/38472793/2745879中所述)
所以实际上在你的情况下,会发生以下情况:
componentDidMount
回调,但状态尚未定义console.log
已被触发,但props
componentDidMount
并更新状态console.log
,现在您的房产是最新的现在,如果要解决此问题,则需要使用每当为组件指定新属性时将调用的componentWillReceiveProps
回调。 https://facebook.github.io/react/docs/react-component.html#componentwillreceiveprops