剧场模式
首页前端React诱人的 react 视频教程-基础篇

诱人的 react 视频教程 - 基础篇 #13 双向数据绑定

海外散仙厉飞雨 · 真仙发布于
5

src/App.js

import React, { Component } from 'react';

import Header from './components/Header';
import Home from './components/Home';

class App extends Component {
  constructor() {
    super();
    this.state = {
      homeLink: "Home"
    }
  }

  onGreet(age) {
    alert(age);
  }

  onChangeLinkName(newName) {
    this.setState({
      homeLink: newName
    })
  }

  render() {
    const user = {
      name: "Anna",
      hobbies: ["Sports", "Reading"]
    }
    return (
      <div className="container">
        <div className="row">
          <div className="col-xs-1 col-xs-offset-11">
            <Header homeLink={this.state.homeLink} />
          </div>
        </div>
        <div className="row">
          <div className="col-xs-1 col-xs-offset-11">
            <h1>Hello !!</h1>
          </div>
        </div>
        <div className="row">
          <div className="col-xs-1 col-xs-offset-11">
            <Home
              name={"Max"}
              initialAge={12}
              user={user}
              greet={this.onGreet}
              changeLink={this.onChangeLinkName.bind(this)}
              initialName={this.state.homeLink}
            />
          </div>
        </div>
      </div>
    );
  }
}

export default App;

src/components/Home.js

import React, { Component } from 'react';

import PropTypes from 'prop-types';

export default class Home extends Component {
  constructor(props) {
    super(props);
    this.state = {
      age: props.initialAge,
      status: 0,
      homeLink: props.initialName
    }
    setTimeout(() => {
      this.setState({
        status: 1
      })
    }, 3000)
  }

  onMakeOlder() {
    this.setState({
      age: this.state.age + 3
    })
  }

  handleGreet() {
    this.props.greet(this.state.age)
  }

  onChangeLink() {
    this.props.changeLink(this.state.homeLink);
  }

  onHandleChange(event) {
    this.setState({
      homeLink: event.target.value
    })
  }

  render() {
    return (
      <div className="container">
        <div className="row">
          <div className="col-xs-1 col-xs-offset-11">
            <div>your name is {this.props.name}, your age is {this.state.age}</div>
            <p>Status: {this.state.status}</p>
            <button onClick={() => {this.onMakeOlder()}} className="btn btn-primary">Make me older</button>
            <hr />
            <button onClick={this.handleGreet.bind(this)} className="btn btn-primary">Greet</button>
            <hr />
            <input
              type="text"
              defaultValue={this.props.initialName}
              value={this.state.initialName}
              onChange={(event) => this.onHandleChange(event)}
            />
            <button onClick={this.onChangeLink.bind(this)} className="btn btn-primary">Change Header Link</button>
          </div>
        </div>
      </div>
    );
  }
}

Home.propTypes = {
  name: PropTypes.string,
  age: PropTypes.number,
  user: PropTypes.object,
  greet: PropTypes.func,
  initialName: PropTypes.string
};
5 条回复
  • bestGao · 真仙
    bestGao · 真仙 #1

    value={this.state.initialName} 大佬,请问在Home.js这里改成value={this.state.homelink}不就是了。不需要设置默认 defaultValue啊

  • LeventLei · 太乙巅峰

    嗯 这样是对的

  • loungcingzeon · 金仙后期

    value={this.state.initialName} 大佬,应该是不是应该如下,这样设置好一点呢。我看你 初始值也没有,

    this.state = {

    initialName:'',
    }

    onHandleChange = (event) => {
    this.setState({
    homeLink: event.target.value,
    initialName: event.target.value,
    })

        console.log(event.target.value);
    }
    

    {this.state.initialName}
    this.onHandleChange(event)}
    />
    Change Header Link

  • guxiaobai · 道祖
    guxiaobai · 道祖 #4

    受控组件

  • hacker0limbo · 真仙

    请教前辈一个问题, 在onHandleChange这个方法里面, 可否写成如下形式:

    onHandleChange(event) {
      this.setState({
        homeLink: event.target.value,
      }, () => {
        this.props.changeLink(this.state.homeLink)
      })
    }
    

    即, 增加一个callback, 保证在state更新以后, 立马更新view, 不用再点击按钮(也就是onChangeLink()这个方法)来手动触发视图的更新? 这样是不是更加直观一点? 当然原理是一样的, 只是个人的想法而已? 谢谢

© 汕尾市求知科技有限公司 | 粤ICP备19038915号 | 在线学员:91

Top