属性更改后,React 组件不会重新渲染
今天在 SorninHeadachePost 中,我们有一个新的 React 渲染,但没有发生。
我将尽力解释并提供尽可能多的信息(如果您认为缺少某些内容,请不要犹豫)。
所以 !
我有一个名为 Navigation
的 React 组件,一旦 props 更改,它应该重新渲染。
//[...]
const [availableViews, setAvailableViews] = useState(props.availableViews)
const [userViews, setUserViews] = useState(props.userViews)
const [_, forceUpdate] = useReducer((x) => x + 1, 0);
useEffect(() => {
setAvailableViews(props.availableViews)
}, [props.availableViews])
useEffect(() => {
setUserViews(props.userViews)
console.log("the log")
forceUpdate()
}, [props.userViews])
return (
<div className="navigation-root">
<div className="navigation-title">
<img src={dashboardSVG} alt="Dashboard"/>
<span className="navigation-titleSpan">DASHBOARD</span>
</div>
<div className="navigation-tabContainer">
{
props.dataInitialized && availableViews.length > 0
?
availableViews.map((d:ViewObject, key:any) => {
if (d.viewName.startsWith("Default ")) {
let views: ViewObject[] = []
for (let i = 0; i<userViews.length;i++) {
//[...]
在此表示 React 组件 Navigation
的代码片段中,属性 userViews
在用户操作时发生更改,抛出日志 (the log
)在这个动作上,所以 prop 被更新。
但没有任何变化......
可能是因为使用状态 userViews
的逻辑被包装到 .map
回调中。 (如果是因为这个,怎么可能重新渲染)
如你所见,我尝试了减速器的 hack 来强制更新。也没有任何改变。
此时,我正在寻找该组件的完整重新渲染,以便它更新所有子组件。
Today in SorninHeadachePost we have a new React render that doesn't occur.
I'll try to explain as best as I can and give maximum informations (do not hesitate if you think something is missing).
SO !
I have a React component named Navigation
that should re-render once it's props change.
//[...]
const [availableViews, setAvailableViews] = useState(props.availableViews)
const [userViews, setUserViews] = useState(props.userViews)
const [_, forceUpdate] = useReducer((x) => x + 1, 0);
useEffect(() => {
setAvailableViews(props.availableViews)
}, [props.availableViews])
useEffect(() => {
setUserViews(props.userViews)
console.log("the log")
forceUpdate()
}, [props.userViews])
return (
<div className="navigation-root">
<div className="navigation-title">
<img src={dashboardSVG} alt="Dashboard"/>
<span className="navigation-titleSpan">DASHBOARD</span>
</div>
<div className="navigation-tabContainer">
{
props.dataInitialized && availableViews.length > 0
?
availableViews.map((d:ViewObject, key:any) => {
if (d.viewName.startsWith("Default ")) {
let views: ViewObject[] = []
for (let i = 0; i<userViews.length;i++) {
//[...]
In this code snippet that represent the React component Navigation
, the prop userViews
changes on a user action, the log (the log
) is being thrown on this action, so the prop is updated.
But nothing change....
May it be because the logic that use the state userViews
is wrapped into a .map
callback. (If it's because of this, how can it be rerendered)
As you can see I tried the hack of the reducer to forceupdate. Nothing changes either.
At this point I'm looking for a full rerender of this component so it updates all child components.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论