我有一个表 - 我们称之为表1.表1中的一排点击正在显示另一个表,我们称之为一个表2.相关的表2显示数据在表1中点击行有时垂直滚动需要显示在表2中,有时不是行数-depends。 需要解决:有边界的不必要的转变不被显示在滚动时:更新终极版道具/状态下的div的onclick
。 该解决方案的想法:“更改保证金的权利”根据条件显示是否滚动退出或不存在。将此条件的结果保存到Redux支持: element.scrollHeight> element.clientHeight || element.scrollWidth>
element.clientWidth
问题:试图从不同的事件作出反应,例如componentDidMount,componentWillReceiveProps更新滚动的显示/非显示到终极版支柱,CopmponentDidUpdate(设置状态导致infinte环这里)以及点击事件。在将道具设置为Redux之后,尝试使用forceUpdate()。
当CONSOLE.LOG成铬(F12),其被正确地关联于显示的唯一结果控制台/滚动条的非显示从componentDidUpdate内的,它不会在了Redux丙反映( isoverflown函数返回true,终极版this.props.scrollStatus和this.state.scrollStatus都是假的)。也不喜欢的document.getElementById对其中包含的行div中使用,因为它打破了DOM的操纵从道具和状态中,但没有找到不同的解决方案现在。
的代码的其余部分:
1)作用:
export function setScrollStatus(scrollStatus) {
return {
type: 'SET_SCROLL_STATUS',
scrollStatus: scrollStatus
};
}
2)减速机:
export function scrollStatus(state = [], action) {
switch (action.type) {
case 'SET_SCROLL_STATUS':
return action.scrollStatus;
default:
return state;
}
}
3)Page.js(请点击图片查看代码)
import {setScrollStatus} from '../actions/relevantfilename';
function isOverflown(element) {
return element.scrollHeight > element.clientHeight ||element.scrollWidth > element.clientWidth;
}
class SportPage extends React.Component {
constructor(props) {
super(props);
this.state = initialState(props);
this.state = {
scrolled:false,
scrollStatus:false};
componentDidUpdate() {
console.log("1 isoverflown bfr redux-this.props.setScrollStatus inside componentDidUpdate",isOverflown(document.getElementById('content')));
//redux props
this.props.setScrollStatus(isOverflown(document.getElementById('content')));
console.log("2 isoverflown aftr redux-this.props.setScrollStatus inside componentDidUpdate",isOverflown(document.getElementById('content')));
//redux props
this.props.scrollStatus ? console.log (" 3 this.props.scrollStatus true inside componentDidUpdate") : console.log("this.props.scrollStatus false inside componentDidUpdate");
console.log ("4 state scrollstatus inside componentDidUpdate" , this.state.scrollStatus)
}
componentDidMount(){
console.log("3 isoverflown bfr set",isOverflown(document.getElementById('content')));
this.props.setScrollStatus("set inside didMount", isOverflown(document.getElementById('content')));
console.log("4 isoverflown aftr set didMount",isOverflown(document.getElementById('content')));
this.props.scrollStatus ? console.log ("scrollStatus true") : console.log("scrollStatus false");
console.log ("state scrollstatus inside didMount" , this.state.scrollStatus)
}
render() {
<div style={{overflowY:'scroll',overflowX:'hidden',height:'50vh',border:'none'}}>
{
this.props.rowData.map((row,index)=>
<div style={{ display: 'flex',flexWrap: 'wrap', border:'1px solid black'}}
onClick={ e => { this.setState({ selected: index, detailsDivVisible: true,scrolled:isOverflown(document.getElementById('content')),
scrollStatus:isOverflown(document.getElementById('content')) },
this.props.setScrollStatus(isOverflown(document.getElementById('content'))),this.forceUpdate(),console.log ("onclick this.state.scrollStatus", this.state.scrollStatus),
console.log ("onclick pure funtion", isOverflown(document.getElementById('content'))));
const mapDispatchToProps = (dispatch) => {
return {
setScrollStatus: function (scrollStaus) {dispatch (setScrollStatus(scrollStaus))},
};
};
export default connect(mapStateToProps, mapDispatchToProps)(Page);
你的代码somowhere像github上左右。将不得不运行该项目来查看问题。 –
很好,你可以解决它。 –
谢谢你的帮助。 – Dani