我遵循了 React/Redux 教程,从我在互联网上的几篇文章中看到的内容中,我意识到内联函数对 React 的性能不利。
据我所知,函数是引用类型,如果您使用内联函数,对于每次重新渲染,该函数将在内存中占据不同的位置。
在教程示例中,我有这个 deleteExperience() 方法,教师使用内联方法。
import React, { Fragment } from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import Moment from 'react-moment';
import { Link, withRouter } from 'react-router-dom';
import { deleteExperience } from '../../actions/profileActions';
const Experience = ({ experience, deleteExperience }) => {
const experiences = experience.map(exp => (
<tr key={exp._id}>
<td>{exp.company}</td>
<td className="hide-sm">{exp.title}</td>
<td>
<Moment format="YYYY/MM/DD">{exp.from}</Moment> -
{exp.to === null ? (
' Now '
) : (
<Moment format="YYYY/MM/DD">{exp.to}</Moment>
)}
</td>
<td>
<button className="btn btn-danger" onClick={() => deleteExperience(exp._id)}>
Delete
</button>
</td>
</tr>
));
return (
<Fragment>
<h2 className="my-2">Experience Credentials</h2>
<table className="table">
<thead>
<tr>
<th>Company</th>
<th className="hide-sm">Title</th>
<th className="hide-sm">Years</th>
<th />
</tr>
</thead>
<tbody>{experiences}</tbody>
</table>
</Fragment>
);
};
Experience.propTypes = {
experience: PropTypes.array.isRequired,
deleteExperience: PropTypes.func
};
export default connect(
null,
{deleteExperience}
)(withRouter(Experience));
所以导师说他用的是内联函数
onClick={() => deleteExperience(exp._id)}
而不仅仅是直接调用函数
onClick={deleteExperience(exp._id)}
不立即执行。
所以,请有人告诉我,如果关于使用内联函数的不良做法的理论是正确的,如何处理这种情况?我尝试了很多方法,都没有成功。