Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSX 解析有所不同 #9

Closed
MuYunyun opened this issue Oct 10, 2018 · 0 comments
Closed

JSX 解析有所不同 #9

MuYunyun opened this issue Oct 10, 2018 · 0 comments

Comments

@MuYunyun
Copy link
Owner

MuYunyun commented Oct 10, 2018

class Todo extends Component {
  constructor() {
    super()
    this.state = {
      items: ['foo', 'bar'],
    }
  }

  render() {
    console.log('renderParent')
    return (
      <div>
        <List items={this.state.items} />
      </div>
    )
  }
}

class List extends Component {
  render() {
    console.log('renderItem', this.props.items)
    return (
      <ul>
        {
          this.props.items.map(item =>
            <li key={item}>{item}</li>
          )
        }
      </ul>
    )
  }
}

下面两种情况 JSX 解析有所不同:

<ul>
  {
    this.props.items.map(item => // 针对这种情况 JSX 有所不同
      <li key={item}>{item}</li>
    )
  }
</ul>

<ul>
  <li>123</li>
  <li>456</li>
</ul>

目前测试的是 this.props.items.map(item => <li>{item}</li>) 这样一层遍历的,后续如果有多层嵌套也许还会有所变化。

另外修复了个由于 '' ? 1 : 0 判断失误的 bug。

完整版测试用例

class Todo extends Component {
  constructor() {
    super()
    this.state = {
      items: ['foo', 'bar'],
    }

    this.add = this.add.bind(this)
  }

  add(value) {
    const items = this.state.items.slice()
    items.push(value)

    this.setState({
      items,
    })
  }

  render() {
    console.log('renderParent', this.state.items)
    return (
      <div>
        <List items={this.state.items} />
        <Form add={this.add} />
      </div>
    )
  }
}

class List extends Component {
  render() {
    console.log('renderItem', this.props)
    return (
      <ul>
        {
          this.props.items.map(item =>
            <li key={item}>{item}</li>
          )
        }
      </ul>
    )
  }
}

class Form extends Component {
  constructor(props) {
    super(props)
    this.state = {
      value: ''
    }

    this.handleChange = this.handleChange.bind(this)
  }

  handleChange({ target }) {
    this.setState({
      value: target.value,
    })
  }

  render() {
    console.log('renderForm', this.props)
    const { add } = this.props
    return (
      <div>
        <input
          value={this.state.value}
          onChange={this.handleChange}
        />
        <button onClick={() => add(this.state.value)}>+</button>
      </div>
    )
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant