zoukankan      html  css  js  c++  java
  • [React] Break up components into smaller pieces using Functional Components

    We are going to ensure our app is structured in a clear way using functional components. Then, we are going to pass those components state values from the parent class component.

    const { Component } = React;
    
    const InputField = (props) => {
      return (
        <input type="text" />
      )
    }
    
    const Button = (props) => {
      return (
        <button>Go</button>
      )
    }
    
    const List = (props) => {
      console.log(props)
      return (
        <ul>
          {props.todos.map(todo => {
            return <li key={todo.id}>{todo.name}</li>
          })}
        </ul>
      )
    }
    
    class App extends Component {
      constructor() {
        super()
        this.state = {
          todos: [
            {id: 0, name: 'foo'},
            {id: 1, name: 'bar'},
            {id: 2, name: 'baz'}
          ]
        }
      }
    
      render() {
        return (
          <div className="App">
            <InputField />
            <Button />
            <List todos={this.state.todos} />    
          </div>
        )
      }
    }
    
    ReactDOM.render(
      <App />,
      document.getElementById('root')
    );
  • 相关阅读:
    map
    构造函数和对象
    for...in...and for each...in...
    事件
    JSON
    css伪类
    正则表达式
    什么是DOM、什么是BOM
    CSS颜色
    grid-layout实验
  • 原文地址:https://www.cnblogs.com/Answer1215/p/6293146.html
Copyright © 2011-2022 走看看