Javascript 组件未正确更新视图
Render确实会被调用,即使调试器显示Javascript 组件未正确更新视图,javascript,reactjs,redux,Javascript,Reactjs,Redux,Render确实会被调用,即使调试器显示temp已正确填充,但更改似乎没有发生(json字符串不会进入dom)。我可能遗漏了一些明显的东西 class ProfileComponent extends Component { constructor(props){ this.props = props; } componentDidMount(){ window.mainStore.subscribe(this.render.bind(
temp
已正确填充,但更改似乎没有发生(json字符串不会进入dom)。我可能遗漏了一些明显的东西
class ProfileComponent extends Component {
constructor(props){
this.props = props;
}
componentDidMount(){
window.mainStore.subscribe(this.render.bind(this))
}
render() {
var temp = JSON.stringify(window.mainStore.getState().profile);
return (
<div>
{temp}
</div>
);
}
}
class ProfileComponent扩展组件{
建造师(道具){
this.props=props;
}
componentDidMount(){
window.mainStore.subscribe(this.render.bind(this))
}
render(){
var temp=JSON.stringify(window.mainStore.getState().profile);
返回(
{temp}
);
}
}
调试看起来像:
第一次呈现“ProfileComponent”时,我们似乎没有subscribe方法,在componentDidMount之后,我们看到了正确的结果,让我们尝试添加一个阻止第一次呈现以返回无效{temp}的状态:
class ProfileComponent extends Component {
constructor(props){
super(props);
this.state = { loading: true, temp:''};
}
componentDidMount() {
window.mainStore.subscribe(); // I don't see all your code but i think here we don't need to bind the render because the component will render after again after changing the state (you can try it both)
const temp = JSON.stringify(window.mainStore.getState().profile); // better keeping 'window' logic here (especially if our app is SSR)
this.setState({loading: false,temp});
}
render() {
const {loading, temp} = this.state;
if(loading) {return (<div>Loading...</div>)}
return (
<div>
{temp}
</div>
);
}
};
class ProfileComponent扩展组件{
建造师(道具){
超级(道具);
this.state={loading:true,temp:''};
}
componentDidMount(){
window.mainStore.subscribe();//我看不到您的所有代码,但我认为这里不需要绑定呈现,因为组件在更改状态后将再次呈现(您可以同时尝试这两种方式)
const temp=JSON.stringify(window.mainStore.getState().profile);//最好在此处保留“窗口”逻辑(特别是如果我们的应用程序是SSR)
this.setState({loading:false,temp});
}
render(){
const{loading,temp}=this.state;
如果(加载){返回(加载…}
返回(
{temp}
);
}
};
看起来您输入了componentDidMount()
?对不起,这不在我的本地代码中,只是在这里漏掉了。当您输入console.log(temp)
时会发生什么?在您的返回语句之前?只是添加了一个图像来显示,这似乎不是问题,只是它没有显示在dom中,我知道这是因为a=“tesT”工作正常