Reactjs 在componentDidMount之后,react生命周期事件停止
我已经用createreact-app命令创建了starter项目。并添加了两个生命周期事件,componentDidMount()工作正常,但componentWillReceiveProps()不触发 index.js:Reactjs 在componentDidMount之后,react生命周期事件停止,reactjs,Reactjs,我已经用createreact-app命令创建了starter项目。并添加了两个生命周期事件,componentDidMount()工作正常,但componentWillReceiveProps()不触发 index.js: ReactDOM.render( <App appState={appState} />, document.getElementById('root') ); 如果需要的话;package.json: { "name": "assasment1"
ReactDOM.render(
<App appState={appState} />,
document.getElementById('root')
);
如果需要的话;package.json:
{
"name": "assasment1",
"version": "0.1.0",
"private": true,
"dependencies": {
"classnames": "^2.2.5",
"mobx": "^3.2.0",
"mobx-react": "^4.2.2",
"react": "^15.6.1",
"react-dom": "^15.6.1"
},
"devDependencies": {
"custom-react-scripts": "0.0.23"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
}
}
我在网上搜索过,但找不到任何解决方案。
组件将接收道具
只会在后续渲染时触发,而不会在第一次渲染时触发。据我所知,您没有更新应用程序中的任何道具或状态
如果添加计数器和按钮以增加状态道具,您将看到组件将接收道具
功能:
class App extends Component {
constructor() {
super();
this.state = { count: 0 };
this.onClick = this.onClick.bind(this);
}
componentWillReceiveProps() {
debugger; // <-- will now fire on every click
}
onClick() {
this.setState({ count: this.state.count + 1 });
}
render() {
return (
<button onClick={this.onClick}>Clicked {this.state.count} times</button>
);
}
}
类应用程序扩展组件{
构造函数(){
超级();
this.state={count:0};
this.onClick=this.onClick.bind(this);
}
组件将接收道具(){
调试器;//组件将接收道具
仅在后续渲染时启动,而不是在第一次渲染时启动。据我所知,您没有更新应用程序中的任何道具或状态
如果添加计数器和按钮以增加状态道具,您将看到组件将接收道具
功能:
class App extends Component {
constructor() {
super();
this.state = { count: 0 };
this.onClick = this.onClick.bind(this);
}
componentWillReceiveProps() {
debugger; // <-- will now fire on every click
}
onClick() {
this.setState({ count: this.state.count + 1 });
}
render() {
return (
<button onClick={this.onClick}>Clicked {this.state.count} times</button>
);
}
}
类应用程序扩展组件{
构造函数(){
超级();
this.state={count:0};
this.onClick=this.onClick.bind(this);
}
组件将接收道具(){
调试器;//我想你应该再读一本
如果组件运行,将调用函数componentDidMount
更新道具时,将调用函数组件将接收道具。)
希望它对你有帮助^ ^我想你应该多读一本
如果组件运行,将调用函数componentDidMount
更新道具时,将调用函数组件将接收道具。)
希望它对您有帮助^^AddcomponentWillUnmount
查看它是否在第一次渲染后卸载,并让我们知道that@MotiAzu我试过了,从来没有进入过那里,也没有一个componentWillReceiveProps()
仅当您发送新的道具时触发。它不会在安装组件时触发,也不会在安装后立即触发。当子组件从父组件接收道具时,将调用componentWillReceiveProps。您可以创建子组件并从应用组件传递道具,然后在子组件内部写入componentWillReceiveProps方法。您将看到调试器停止在那里。但我已经在执行此操作,让我使用origin postAddcomponent willunmount更新它,以查看它是否在第一次渲染后卸载,并让我们知道that@MotiAzu我试过了,从来没有进入过那里,也没有一个componentWillReceiveProps()
仅当您发送新的道具时触发。它不会在安装组件时触发,也不会在安装后立即触发。当子组件从父组件接收道具时,将调用componentWillReceiveProps。您可以创建子组件并从应用组件传递道具,然后在子组件内部写入componentWillReceiveProps方法。您将看到调试器停止在那里。但我已经在做这件事了,让我用origin post更新它“更新道具时将调用function componentWillReceiveProps:)“尽管upper post很有帮助,但这正是我要找的,谢谢!”更新道具时将调用function componentWillReceiveProps:)尽管上面的帖子帮助很大,但这正是我想要的,谢谢!
class App extends Component {
constructor() {
super();
this.state = { count: 0 };
this.onClick = this.onClick.bind(this);
}
componentWillReceiveProps() {
debugger; // <-- will now fire on every click
}
onClick() {
this.setState({ count: this.state.count + 1 });
}
render() {
return (
<button onClick={this.onClick}>Clicked {this.state.count} times</button>
);
}
}