更新 Redux 状态然后在同一实例中获取更新后的状态
Update Redux State & Then Get The Updated State In Same Instance
场景:
我有一个带有子图像的响应式 div
容器(CSS - 宽度:33%)。不想让用户向下滚动,我想找出 div
的绝对尺寸,这样我就可以计算出可以容纳的图像数量(具有固定的宽度和高度)。然后只渲染适合屏幕的图像。
我正在为此应用程序使用 React 和 Redux。这是我正在考虑的逻辑,它不起作用。
_ Smart Component (subscribed to the Store)
|___ `div` container (presentational component)
|______ images (presentational components)
我渲染 'presentational' 组件,然后在 componentDidMount
中,我派发一个函数来找出 div
容器的宽度和高度,并使用 'image capacity' 值(div
容器中可以存储多少张图片)。
在上述函数之后,虽然仍在 componentDidMount
,但我派发了另一个函数,该函数应该从存储(图像容量)中获取 UPDATED 值并更新存储图像数据。
想法是,当商店更新时,智能组件将重新呈现展示组件(div
和图像),因此会发生第二轮重新呈现以显示图像。
不幸的是,这是有缺陷的。传递给展示组件的道具是 'pre-update' 存储值 (0) 所以当我 运行 调度第二个函数用图像更新存储时,它在其中的 'image capacity' 值道具仍然是零 (0)。
我想知道这里要实现什么正确的逻辑?
以防万一,代码更有意义,这里是演示组件代码:
const PreviewTemParent = React.createClass({
componentDidMount : function() {
let elePreviewParent = ReactDOM.findDOMNode( this.refs.previewParent );
previewTemParentAttr.width = elePreviewParent.clientWidth;
previewTemParentAttr.height = elePreviewParent.clientHeight;
this.props.findImgCapacity();
this.temImgToShow( "body" );
},
temImgToShow : function( templateType ) {
let pagination = this.props.previewTemState.get( "pagination" );
let imgCapacity = this.props.previewTemState.get( "imgCapacity" );
console.log( "%c In `PreviewTemParent`, pag, imgCap & props are...", "color : gold", pagination, "; ", imgCapacity, "; ", this.props );
this.props.temImgToShow( templateType, pagination, imgCapacity );
},
render : function() {
return(
<div
className = "previewParent"
ref = "previewParent">
<div className = "previewContainer">
{ this.props.previewTemState.get( "temImg" ).map( ( imgObj ) => {
<PreviewTemImgContainer data = { imgObj } />;
} ) }
</div>
</div>
);
}
});
编辑:
应@pierrepinard_2的要求,我发布了详细的代码,希望能够解释我的尝试。
我尝试了一些方法,包括向 <PreviewTemParent>
的父级添加 'smart component',然后让父级计算尺寸。然而,在那种情况下,道具仍然没有更新,这是可以理解的,因为 componentDidMount 在 <PreviewTemParent>
被安装后是 运行。
这是我根据@pierrepinard_2关于使用componentWillReceiveProps
的建议尝试的解决方案,它似乎没有被调用。
C_PreviewTemParent 容器(智能组件)
import { connect } from "react-redux";
import { temImgToDisplayInContainer } from "../modcon/Actions.js";
import PreviewTemParent from "../component/temContainer/PreviewTemParent.jsx";
const mapStateToProps = ({ previewTemImgState }) => {
return({
previewTemState : previewTemImgState
});
};
const mapDispatchToProps = ( dispatch ) => {
return({
temImgToDisplayInContainer : ( templateType, activePage ) => {
dispatch( temImgToDisplayInContainer( templateType, activePage ) );
}
});
};
export const C_PreviewTemParent = connect( mapStateToProps, mapDispatchToProps )( PreviewTemParent );
预览父组件
const PreviewTemParent = React.createClass({
componentDidMount : function() {
this.props.temImgToDisplayInContainer( "body" );
},
componentWillReceiveProps : function( nextProps ) {
console.log( "%c componentWillReceiveProps is...", "color: green", nextProps );
},
render : function() {
return(
<div className = "previewParent">
<div className = "previewContainer">
{ this.props.previewTemState.get( "temImg" ).map( ( imgObj ) => {
<PreviewTemImgContainer data = { imgObj } />;
} ) }
</div>
</div>
);
}
});
动作创作者
请原谅冗长的代码:!
export const temImgToDisplayInContainer = ( templateType, activePage ) => {
// temImgCapacity finds out the number of images that can
// fit in the container based on the container's dynamic Width & Height.
// in order to make the func reusable, activePage is an optional parameter
// hence the `if`
let temImgCapacity;
if( activePage ){
temImgCapacity = findImgCapacityInPreviewTem( false );
} else {
temImgCapacity = findImgCapacityInPreviewTem( true );
}
// `previewTemImgData` is a store of static data of all the images.
// during development, this is being used instead of ajax calls etc
// `temImgData` stores the data only for the 'templates' we are interested in
let temImgData = previewTemImgData.get( templateType );
let counter = 1;
// `noOfTemplate` is how many images we will render
let noOfTemplate = temImgCapacity.get( "imgCapacity" );
let currentPage = activePage || temImgCapacity.get( "activePage" );
// `maxCounter` is for pagination, which is the last template to store
let maxCounter = currentPage * noOfTemplate;
let temStartFrom = (( currentPage * noOfTemplate ) - noOfTemplate ); // what template start number.
// get the tempales we are only interested in
let filteredTemImgData = temImgData.filter( ( templateObj ) => {
if (( counter <= maxCounter ) && ( counter >= temStartFrom )) {
counter++;
return( templateObj );
}
} );
// merging the data from above first line 'temImgCapacity' & the filtered
// temImgData
let payloadData = filteredTemImgData.merge(( temImgCapacity ));
return({
type : CURRENT_TEM_IMG,
payload : payloadData
});
};
最后,减速器:
const previewTemImgState = ( state = initialPreviewTemState, action ) => {
switch( action.type ) {
case( FIND_IMG_CAPACITY_IN_PREVIEW_TEM ) :
return(
state.set( "imgCapacity", action.payload.imgCapacity )
);
case( RENDER_TEM_IMG ) :
console.log( action.payload );
var newState = state.set( "temImg", action.payload.temImg );
console.log( "%c previewTemImgState is...", "color : red", state.get( "temImg" ), " ; ", newState.get( "temImg" ) );
return(
state.set( "temImg", action.payload )
);
default :
return state;
}
};
问题是当你在temImgToShow()
中调用this.props.previewTemState
时,状态对象引用仍然和componentDidMount()
执行开始时一样,在this.props.findImgCapacity()
之前被调用了。
一种选择是从 componentDidMount()
中仅触发一个合成操作,参数如下:宽度和高度(用于计算图像容量)、模板类型、分页。那么你只会得到一个 UI 具有所需状态的更新。
如果你真的不能只触发一个动作来执行所有工作,那么你可以在 componentWillReceiveProps()
中触发第二个动作,正如 Honza Haering 在评论中建议的那样:
componentDidMount: function() {
let elePreviewParent = ReactDOM.findDOMNode( this.refs.previewParent );
previewTemParentAttr.width = elePreviewParent.clientWidth;
previewTemParentAttr.height = elePreviewParent.clientHeight;
this.props.findImgCapacity();
},
componentWillReceiveProps: function(nextProps) {
let newImgCapacity = nextProps.previewTemState.get( "imgCapacity" );
let oldImgCapacity = this.props.previewTemState.get( "imgCapacity" );
if (newImgCapacity !== oldImgCapacity && newImgCapacity > 0) {
let pagination = nextProps.previewTemState.get( "pagination" );
nextProps.temImgToShow("body", pagination, newImgCapacity);
}
},
或者,如果您使用 redux-thunk 中间件,您可以使用带有承诺的异步操作创建器来正确地链接操作。如果您给我们您的操作代码,我可以告诉您如何操作。
场景:
我有一个带有子图像的响应式 div
容器(CSS - 宽度:33%)。不想让用户向下滚动,我想找出 div
的绝对尺寸,这样我就可以计算出可以容纳的图像数量(具有固定的宽度和高度)。然后只渲染适合屏幕的图像。
我正在为此应用程序使用 React 和 Redux。这是我正在考虑的逻辑,它不起作用。
_ Smart Component (subscribed to the Store)
|___ `div` container (presentational component)
|______ images (presentational components)
我渲染 'presentational' 组件,然后在 componentDidMount
中,我派发一个函数来找出 div
容器的宽度和高度,并使用 'image capacity' 值(div
容器中可以存储多少张图片)。
在上述函数之后,虽然仍在 componentDidMount
,但我派发了另一个函数,该函数应该从存储(图像容量)中获取 UPDATED 值并更新存储图像数据。
想法是,当商店更新时,智能组件将重新呈现展示组件(div
和图像),因此会发生第二轮重新呈现以显示图像。
不幸的是,这是有缺陷的。传递给展示组件的道具是 'pre-update' 存储值 (0) 所以当我 运行 调度第二个函数用图像更新存储时,它在其中的 'image capacity' 值道具仍然是零 (0)。
我想知道这里要实现什么正确的逻辑?
以防万一,代码更有意义,这里是演示组件代码:
const PreviewTemParent = React.createClass({
componentDidMount : function() {
let elePreviewParent = ReactDOM.findDOMNode( this.refs.previewParent );
previewTemParentAttr.width = elePreviewParent.clientWidth;
previewTemParentAttr.height = elePreviewParent.clientHeight;
this.props.findImgCapacity();
this.temImgToShow( "body" );
},
temImgToShow : function( templateType ) {
let pagination = this.props.previewTemState.get( "pagination" );
let imgCapacity = this.props.previewTemState.get( "imgCapacity" );
console.log( "%c In `PreviewTemParent`, pag, imgCap & props are...", "color : gold", pagination, "; ", imgCapacity, "; ", this.props );
this.props.temImgToShow( templateType, pagination, imgCapacity );
},
render : function() {
return(
<div
className = "previewParent"
ref = "previewParent">
<div className = "previewContainer">
{ this.props.previewTemState.get( "temImg" ).map( ( imgObj ) => {
<PreviewTemImgContainer data = { imgObj } />;
} ) }
</div>
</div>
);
}
});
编辑:
应@pierrepinard_2的要求,我发布了详细的代码,希望能够解释我的尝试。
我尝试了一些方法,包括向 <PreviewTemParent>
的父级添加 'smart component',然后让父级计算尺寸。然而,在那种情况下,道具仍然没有更新,这是可以理解的,因为 componentDidMount 在 <PreviewTemParent>
被安装后是 运行。
这是我根据@pierrepinard_2关于使用componentWillReceiveProps
的建议尝试的解决方案,它似乎没有被调用。
C_PreviewTemParent 容器(智能组件)
import { connect } from "react-redux";
import { temImgToDisplayInContainer } from "../modcon/Actions.js";
import PreviewTemParent from "../component/temContainer/PreviewTemParent.jsx";
const mapStateToProps = ({ previewTemImgState }) => {
return({
previewTemState : previewTemImgState
});
};
const mapDispatchToProps = ( dispatch ) => {
return({
temImgToDisplayInContainer : ( templateType, activePage ) => {
dispatch( temImgToDisplayInContainer( templateType, activePage ) );
}
});
};
export const C_PreviewTemParent = connect( mapStateToProps, mapDispatchToProps )( PreviewTemParent );
预览父组件
const PreviewTemParent = React.createClass({
componentDidMount : function() {
this.props.temImgToDisplayInContainer( "body" );
},
componentWillReceiveProps : function( nextProps ) {
console.log( "%c componentWillReceiveProps is...", "color: green", nextProps );
},
render : function() {
return(
<div className = "previewParent">
<div className = "previewContainer">
{ this.props.previewTemState.get( "temImg" ).map( ( imgObj ) => {
<PreviewTemImgContainer data = { imgObj } />;
} ) }
</div>
</div>
);
}
});
动作创作者 请原谅冗长的代码:!
export const temImgToDisplayInContainer = ( templateType, activePage ) => {
// temImgCapacity finds out the number of images that can
// fit in the container based on the container's dynamic Width & Height.
// in order to make the func reusable, activePage is an optional parameter
// hence the `if`
let temImgCapacity;
if( activePage ){
temImgCapacity = findImgCapacityInPreviewTem( false );
} else {
temImgCapacity = findImgCapacityInPreviewTem( true );
}
// `previewTemImgData` is a store of static data of all the images.
// during development, this is being used instead of ajax calls etc
// `temImgData` stores the data only for the 'templates' we are interested in
let temImgData = previewTemImgData.get( templateType );
let counter = 1;
// `noOfTemplate` is how many images we will render
let noOfTemplate = temImgCapacity.get( "imgCapacity" );
let currentPage = activePage || temImgCapacity.get( "activePage" );
// `maxCounter` is for pagination, which is the last template to store
let maxCounter = currentPage * noOfTemplate;
let temStartFrom = (( currentPage * noOfTemplate ) - noOfTemplate ); // what template start number.
// get the tempales we are only interested in
let filteredTemImgData = temImgData.filter( ( templateObj ) => {
if (( counter <= maxCounter ) && ( counter >= temStartFrom )) {
counter++;
return( templateObj );
}
} );
// merging the data from above first line 'temImgCapacity' & the filtered
// temImgData
let payloadData = filteredTemImgData.merge(( temImgCapacity ));
return({
type : CURRENT_TEM_IMG,
payload : payloadData
});
};
最后,减速器:
const previewTemImgState = ( state = initialPreviewTemState, action ) => {
switch( action.type ) {
case( FIND_IMG_CAPACITY_IN_PREVIEW_TEM ) :
return(
state.set( "imgCapacity", action.payload.imgCapacity )
);
case( RENDER_TEM_IMG ) :
console.log( action.payload );
var newState = state.set( "temImg", action.payload.temImg );
console.log( "%c previewTemImgState is...", "color : red", state.get( "temImg" ), " ; ", newState.get( "temImg" ) );
return(
state.set( "temImg", action.payload )
);
default :
return state;
}
};
问题是当你在temImgToShow()
中调用this.props.previewTemState
时,状态对象引用仍然和componentDidMount()
执行开始时一样,在this.props.findImgCapacity()
之前被调用了。
一种选择是从 componentDidMount()
中仅触发一个合成操作,参数如下:宽度和高度(用于计算图像容量)、模板类型、分页。那么你只会得到一个 UI 具有所需状态的更新。
如果你真的不能只触发一个动作来执行所有工作,那么你可以在 componentWillReceiveProps()
中触发第二个动作,正如 Honza Haering 在评论中建议的那样:
componentDidMount: function() {
let elePreviewParent = ReactDOM.findDOMNode( this.refs.previewParent );
previewTemParentAttr.width = elePreviewParent.clientWidth;
previewTemParentAttr.height = elePreviewParent.clientHeight;
this.props.findImgCapacity();
},
componentWillReceiveProps: function(nextProps) {
let newImgCapacity = nextProps.previewTemState.get( "imgCapacity" );
let oldImgCapacity = this.props.previewTemState.get( "imgCapacity" );
if (newImgCapacity !== oldImgCapacity && newImgCapacity > 0) {
let pagination = nextProps.previewTemState.get( "pagination" );
nextProps.temImgToShow("body", pagination, newImgCapacity);
}
},
或者,如果您使用 redux-thunk 中间件,您可以使用带有承诺的异步操作创建器来正确地链接操作。如果您给我们您的操作代码,我可以告诉您如何操作。