官术网_书友最值得收藏!

  • React Components
  • Christopher Pitt
  • 271字
  • 2021-07-09 19:34:45

Component life cycle methods

There are a couple of tricks I want to show you before we wrap up. The first is a life cycle method we can use to tell when a component's properties will change. We can use this to change the appearance of a component, or refresh the internal state.

We can add this method to PageEditor, for example:

class PageEditor extends Component {
    constructor(props) {
        super(props);

        this.state = {
            "changed": false
        };

        this.bind(
            "onCancel",
            "onUpdate"
        );
    }

    isChanged(next, previous) {
 return JSON.stringify(next) !== JSON.stringify(previous)
 }

 componentWillReceiveProps(props) {
 this.setState({
 "changed": this.isChanged(props, this.props)
 });
 }

    render() {
        return <p>
            <input
                type="text"
                name="title"
                value={this.props.title}
                onChange={this.onUpdate}
                />
            <textarea
                name="body"
                value={this.props.body}
                onChange={this.onUpdate}>
            </textarea>
            <button
                onClick={this.onCancel}>
                cancel
            </button>
        </p>;
    }

    onUpdate() {
        this.props.onUpdate(
            this.props.id,
            event.target.name,
            event.target.value
        );
    }

 onCancel(event) {
 event.preventDefault();
 this.props.onCancel();
 }
}

We can now tell when the page changes, even though the changes are immediately propagated.

Another magic method we can use will help cut down on the comparisons React needs to perform. It's called shouldComponentUpdate and we can add it to PageView:

class PageView extends Component {
    constructor(props) {
        super(props);

        this.bind(
            "onDelete"
        );
    }

    isChanged(next, previous) {
 return JSON.stringify(next) !== JSON.stringify(previous)
 }
 
 shouldComponentUpdate(props, state) {
 return this.isChanged(props, this.props);
 }

    render() {
        return <p>
            {this.props.title}
            <button
                onClick={this.props.onEdit}
                >edit</button>
            <button
                onClick={this.onDelete}
                >delete</button>
        </p>;
    }

    onDelete() {
        this.props.onDelete(
            this.props.id
        );
    }
}

The shouldComponentUpdate method gives us a way to tell React not to look for changes in this component. At this scale, we're not likely to see huge performance improvements. But when we add this method to more complex layouts, it will drastically reduce the amount of work required to work out how the document should change.

We'll be using these tricks later, as we build more complex content management features.

主站蜘蛛池模板: 新巴尔虎右旗| 麻城市| 普宁市| 肇源县| 阳新县| 临澧县| 桃江县| 潍坊市| 潍坊市| 宁津县| 上栗县| 达拉特旗| 东乡县| 铁岭县| 揭阳市| 外汇| 合肥市| 宁蒗| 双峰县| 明光市| 永宁县| 兴安县| 安西县| 岑溪市| 凉城县| 子长县| 海林市| 招远市| 静宁县| 木兰县| 迁安市| 连江县| 钟山县| 富源县| 安泽县| 嵊州市| 博爱县| 郧西县| 丹寨县| 乐山市| 贡山|