gpt4 book ai didi

javascript - ReactJS - 在 ajax 函数呈现数据后执行 javascript 代码

转载 作者:行者123 更新时间:2023-11-30 14:24:33 27 4
gpt4 key购买 nike

我应该把 JavaScript 代码放在哪里,以便在渲染完成后调用插件。

<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>

我把它添加到

  componentDidUpdate(prevProps, prevState) {
alert();
};

然后,在第一个元素之后,警报功能开始工作,

这些是我的实际文件 - https://drive.google.com/drive/folders/1G7UOaCFS_521hfZc4-jNeLGiKR4XaQjP?usp=sharing

enter image description here

enter image description here

最佳答案

如果我正确理解您的要求,那么 componentDidUpdate() Hook 将是用于访问 DOM(并调用您的插件)的正确 Hook 。这将在您的组件呈现后调用。

作为state in the official docs对于 componentDidUpdate():

Use this as an opportunity to operate on the DOM when the component has been updated. This is also a good place to do network requests as long as you compare the current props to previous props (e.g. a network request may not be necessary if the props have not changed).

需要注意的关键是 componentDidUpdate() 钩子(Hook)不会在第一次渲染时触发。

更新

要解决这个“首次渲染”问题,您可以使用可以传递给 setState(state, callback) 的可选回调。此回调 在您的组件呈现后(在状态更改之后)触发。

在你的情况下,你可以这样做:

loadData() {
axios.get(REST_API).then((response) => {
//console.log(response.data);
this.setState({ users: response.data }, () => {

// When this callback is invoked, the component has been
// rendered and the DOM can be safely accessed
})
});
}

关于javascript - ReactJS - 在 ajax 函数呈现数据后执行 javascript 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52158612/

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