【发布时间】:2021-01-13 08:47:08
【问题描述】:
我很难弄清楚为什么我在 WorkitemHistory 的 div 中的组件 HistoriesList 没有呈现其内容,尽管我看到了要打印的 console.log。我的代码是:
import React, { useEffect, useRef, useContext } from 'react';
import { fetchHistories } from 'stores/actions';
import { ProjectContext } from 'AppContexts/ProjectContext';
export default function WorkitemHistory({ isOpened, closeModal, curWorkitem }) {
const [state, dispatch] = useContext(ProjectContext);
const { histories } = state;
const modalRef = useRef(null);
const $modalEl = useRef(null);
const jQuery = window.$;
useEffect(() => {
$modalEl.current = jQuery(modalRef.current);
$modalEl.current.modal({
keyboard: false,
backdrop: 'static',
show: false,
});
if (isOpened) {
$modalEl.current.modal('show');
}
}, [isOpened, $modalEl]);
useEffect(() => {
(async () =>
await fetchHistories(dispatch, { workitemId: curWorkitem }, true))();
}, []);
const handleModalClose = (e) => {
e.preventDefault();
$modalEl.current.modal('hide');
$modalEl.current.on('hidden.bs.modal', function (e) {
closeModal();
});
};
if (histories.isFetching) return 'Loading..';
console.log('Workitem histories', histories);
return (
<div
className='modal fade'
id='workitemHistoriesModal'
tabindex='-1'
role='dialog'
ref={modalRef}
>
<div className='modal-dialog modal-lg' role='document'>
<div className='modal-content'>
<div className='modal-header'>
<button className='close' type='button' onClick={handleModalClose}>
<span>×</span>{' '}
</button>
<h3 className='modal-title'>Work Item - History</h3>
</div>
<div className='modal-body'>
<div className='alert alert-info'>
Below you find a list of changes that were applied to the
workitem.
</div>
<div>
<HistoriesList histories={histories} curWorkitem={curWorkitem} />
</div>
</div>
<div className='modal-footer'>
<button className='btn btn-primary' onClick={handleModalClose}>
Close
</button>
</div>
</div>
</div>
</div>
);
}
function HistoriesList({ histories, curWorkitem }) {
const rows = histories.byId[curWorkitem];
if (rows === void 0 || rows.length === 0) return null;
console.log(
'Type of',
rows.map((row) => row.author)
);
return rows.map((row) => <div key={row.author}>{row.author}</div>);
}
附上屏幕截图,您将在那里看到模态框为空。看起来很简单,但我现在已经厌倦了弄清楚这个奇怪的事情的原因是什么。请帮忙。如果我需要提供更多信息,请告诉我。
编辑
按照评论中的要求添加了console.log。
function HistoriesList({ histories, curWorkitem }) {
const rows = histories.byId[curWorkitem];
console.log('HistoriesList', rows);
if (rows === void 0 || rows.length === 0) return null;
console.log(
'Type of',
rows.map((row) => row.author)
);
return rows.map((row) => <div key={row.author}>{row.author}</div>);
}
编辑2
我做了这样的代码更改..
function HistoriesList({ histories, curWorkitem }) {
const rows = histories.byId[curWorkitem];
console.log('HistoriesList', rows);
if (rows === void 0 || rows.length === 0) return 'hello';
console.log(
'Type of',
rows.map((row) => row.author)
);
return 'world';
chrome控制台证明是在if之后来的,但是modal没有文字变化。
【问题讨论】:
-
可能是 CSS 问题,只需检查 DOM 并检查它是否在元素选项卡中呈现。
-
在
if (rows === void 0 || rows.length === 0) return null;之前放置一个console.log(rows)以查看rows是否正在重置 -
@tsfahmad 我查过了,
<div>是空的。 -
@TaghiKhavari 按照您的要求添加了 console.log。
-
可能是由于模型被你的 jquery 控制,然后你尝试更新一个 jquery 框的内容。尝试删除 jquery 并在反应中做所有事情。
标签: javascript reactjs