javascript - ReactJS - execute a javascript code after the ajax function renders the data
问题描述
Where should I place the JavaScript code, so that I can call a plugin after the render is completed.
<html>
<head>
<script src='https://cdnjs.cloudflare.com/ajax/libs/es6-promise/3.2.2/es6-promise.min.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/react/15.2.1/react.min.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/react/15.2.1/react-dom.min.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/axios/0.13.1/axios.min.js'></script>
<script src="https://unpkg.com/babel-standalone@6.15.0/babel.min.js"></script>
</head>
<body>
<div id="root"></div>
<script type="text/babel">
const REST_API = 'users.json';
// The root component
const App = props => (
<div className="app">
<UserListContainer />
</div>
);
// Container
class UserListContainer extends React.Component {
constructor(props) {
super(props);
this.state = {
users: [{
name: 'Loading data...'
}]
};
}
loadData() {
axios.get(REST_API).then((response) => {
//console.log(response.data);
this.setState({
users: response.data
})
});
}
// Life cycles hooks
// facebook.github.io/react/docs/component-specs.html
componentWillMount() {};
componentDidMount() {
// Data is loaded async, therefore will be inserted after first DOM rendering
this.loadData();
};
componentWillReceiveProps(nextProps) {};
shouldComponentUpdate(nextProps, nextState) {
return true;
};
componentWillUpdate(nextProps, nextState) {};
componentDidUpdate(prevProps, prevState) {};
componentWillUnmount() {};
render() {
return (<UserList users={this.state.users} />);
};
}
// Presentation
const UserItem = (user, index) => (
<li key={index}>
<div className="header">
<div className="name"> {user.name}</div>
<div className="index">{(index+1)}</div>
</div>
<div className="date">
<i className="fa fa-date" aria-hidden="true"></i>
<span>{user.date}</span>
</div>
<div className="contact">
<i className="fa fa-phone" aria-hidden="true"></i>
<span>{user.phone}</span>
</div>
</li>
);
const UserList = props => (
<div className="user-list">
<h1>React ES6 Ajax, Container and Presentation - example</h1>
<ul>
{props.users.map(UserItem)}
</ul>
</div>
);
// Render
ReactDOM.render(
<App />, document.getElementById('root')
);
</script>
</body>
</html>
I added it to
componentDidUpdate(prevProps, prevState) {
alert();
};
Then , after the first element, the alert function is working,
These are my actual files - https://drive.google.com/drive/folders/1G7UOaCFS_521hfZc4-jNeLGiKR4XaQjP?usp=sharing
解决方案
componentDidMount
是获取数据的正确位置,并且您正在以正确的方式进行操作。componentDidUpdate
如果您需要接收道具或状态并相应地更新,则使用钩子。如果您的子组件要通过接收数据从父组件更新它们,则这是必需的。但是在查看您的代码后,我没有注意到您需要父组件。所以,你不需要componentDidUpdate
钩子。您只需要执行以下操作。
保持如下状态:
this.state = {
users: [],
loading: true,
loadingText: 'Loading data...'
};
然后,在您将数据集加载为 false 之后:
loadData() {
axios.get(REST_API).then((response) => {
//console.log(response.data);
this.setState({
users: response.data,
loading: false
})
});
}
现在,当你渲染它们时:
const UserList = props => (
<div className="user-list">
<h1>React ES6 Ajax, Container and Presentation - example</h1>
<ul>
{!props.loading && props.users && props.users.map(UserItem)}
</ul>
</div>
);
注意:您还必须通过加载状态。
推荐阅读
- r - 更改 tbl_regression/tbl_uvregression 和 tbl_summary 中的默认变量值显示?#gtsummary
- elasticsearch - 如何在elasticsearch中做两个嵌套对象聚合?
- excel - 模糊字符串匹配 Excel
- scala - 从细化中获取正在细化的类
- ios - 在单击事件后更改 UiButton ImageView.alpha 中,如何保留以前的 alpha?
- kubernetes - 如何在 kubernetes 中调试与 istio 的 mTLS 通信?
- c# - 获取点后有多少个十进制数字
- python - 没有这样的文件或目录:'gdal-config'
- java - rabbitMQ - rabbitmq 面板中的消息计数
- javascript - 为什么 formvalidation.io 插件不适用于 Vue 3?