gpt4 book ai didi

reactjs - 上下文提供者不触发 this.props.children 的重新渲染

转载 作者:行者123 更新时间:2023-12-05 05:10:28 32 4
gpt4 key购买 nike

我正在使用上下文提供程序 (AccountProvider.js) 作为向其所有子组件提供帐户详细信息(以及通过 socket.io 连接的通知)的一种方式。通知和用户对象可以很好地加载到 AccountProvider 组件中,并且实际上会不断更新自己的状态。

知道如果父组件的状态发生变化,这应该意味着父组件的所有子组件也应该重新渲染,对吧?显然这里不是这种情况,我似乎无法弄清楚为什么。

这个问题有什么解决办法吗?也许需要强制更新子组件?

AccountProvider.js

import React, { Component } from 'react'
import axios from 'axios'
import socketIOClient from "socket.io-client";
import config from '../config/config'

export const myContext = React.createContext();

export default class AccountProvider extends Component {

state = {
data: []
}

async componentDidMount() {
const auth_token = localStorage.getItem('jwt token')
const header = 'Bearer ' + auth_token;
const res = await axios.get(config.API_URL+'/api/account', {headers: {Authorization:header}});
const blob = await axios.get(config.API_URL+'/' + res.data.image, {headers: {Authorization:header}, responseType: "blob"});
var user = res.data;
user["image"] = URL.createObjectURL(blob.data);

this.setState({
data: {user}
});

const io = socketIOClient(config.REDIS_URL, { query: {jwt: localStorage.getItem('jwt token')} });

io.on("event", data => {
if (data !== this.state.notifications){
var curr_data = this.state.data;
curr_data["notifications"] = data;
this.setState({data: curr_data});
}
});

io.on("reconnect", () => {
io.emit("jwt", localStorage.getItem('jwt token'));
})
}


render() {
return (
<myContext.Provider value={this.state.data}>
{this.props.children}
</myContext.Provider>
)
}
}

App.js(渲染)

  <AccountProvider>
<Route
path="/dashboard"
render={({ match: { url } }) => (
<Dashboard>
<Switch>
<Route path={`${url}/Account`} component={Account} exact />
<Route path={`${url}/Deposits`} component={Deposits} exact />
<Route path={`${url}/Notifications`} component={Client} exact />
<Route path={`${url}/`} component={Account} exact /> */}
<Route component={Page404Dashboard} />
</Switch>
</Dashboard>
)}/>
</AccountProvider>

Dashboard.js

import React, { Component } from 'react'
import AccountContent from './AccountContent';
import PendingCard from '../PendingCard';
import {myContext} from '../../../AccountProvider'

class Dashboard extends Component {
render() {
// The following should log notifications after AccountProvider rerenders
console.log(this.context.notifications && this.context.notifications);
return (
<>
{ this.props.hasCard ? <AccountContent/> : <PendingCard/> }
</>
)
}
}

Dashboard.contextType = myContext;
export default Dashboard;

最佳答案

改变:

var curr_data = this.state.data;
curr_data["notifications"] = data;
this.setState({data: curr_data});

与:

var curr_data = Object.assign({}, this.state.data);
curr_data["notifications"] = data;
this.setState({data: curr_data});

你并没有真正用第一种情况更新状态

关于reactjs - 上下文提供者不触发 this.props.children 的重新渲染,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56623290/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com