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.

  1. const { Component } = React;
  2.  
  3. const InputField = (props) => {
  4. return (
  5. <input type="text" />
  6. )
  7. }
  8.  
  9. const Button = (props) => {
  10. return (
  11. <button>Go</button>
  12. )
  13. }
  14.  
  15. const List = (props) => {
  16. console.log(props)
  17. return (
  18. <ul>
  19. {props.todos.map(todo => {
  20. return <li key={todo.id}>{todo.name}</li>
  21. })}
  22. </ul>
  23. )
  24. }
  25.  
  26. class App extends Component {
  27. constructor() {
  28. super()
  29. this.state = {
  30. todos: [
  31. {id: 0, name: 'foo'},
  32. {id: 1, name: 'bar'},
  33. {id: 2, name: 'baz'}
  34. ]
  35. }
  36. }
  37.  
  38. render() {
  39. return (
  40. <div className="App">
  41. <InputField />
  42. <Button />
  43. <List todos={this.state.todos} />
  44. </div>
  45. )
  46. }
  47. }
  48.  
  49. ReactDOM.render(
  50. <App />,
  51. document.getElementById('root')
  52. );

[React] Break up components into smaller pieces using Functional Components的更多相关文章

  1. [Vue @Component] Write Vue Functional Components Inline

    Vue's functional components are small and flexible enough to be declared inside of .vue file next to ...

  2. [React] Return a list of elements from a functional component in React

    We sometimes just want to return a couple of elements next to one another from a React functional co ...

  3. [Vue] Load components when needed with Vue async components

    In large applications, dividing the application into smaller chunks is often times necessary. In thi ...

  4. [React] Refactor a Stateful List Component to a Functional Component with React PowerPlug

    In this lesson we'll look at React PowerPlug's <List /> component by refactoring a normal clas ...

  5. React 中的函数式思想

    函数式编程简要概念 函数式编程中一个核心概念之一就是纯函数,如果一个函数满足一下几个条件,就可以认为这个函数是纯函数了: 它是一个函数(废话): 当给定相同的输入(函数的参数)的时候,总是有相同的输出 ...

  6. TED演讲:别不信,你只需20个小时,就能学会任何事情!

    https://www.bilibili.com/video/av50668972/?spm_id_from=333.788.videocard.3 two years ago, my life ch ...

  7. type Props={};

    Components Learn how to type React class components and stateless functional components with Flow Se ...

  8. [React] Make Compound React Components Flexible

    Our current compound component implementation is great, but it's limited in that users cannot render ...

  9. [React] Intro to inline styles in React components

    React lets you use "inline styles" to style your components; inline styles in React are ju ...

随机推荐

  1. JavaScript--数据结构与算法之排序

    排序总结————常见的排序 常见的9中排序(冒泡,选择,插入(二分插入,希尔),归并,快速,堆,计数,基数,桶排序)可分为两类 比较排序:冒泡,选择,插入(二分插入,希尔),归并,堆,快速 非比较排序 ...

  2. python 新模块或者包的安装方法

    主要介绍通过pip自动工具来安装需要的包. 1,先安装pip 下载pip的包(包括setup.py文件) cmd载入到pip本地文件所在路径,使用命令进行安装. python setup.py ins ...

  3. 洛谷——U10783 名字被和谐了

    https://www.luogu.org/problem/show?pid=U10783 题目背景 众所周知,我们称g是a的约数,当且仅当g是正数且a mod g = 0. 众所周知,若g既是a的约 ...

  4. OC文件操作、获取文件属性

    #import <Foundation/Foundation.h> //获取文件的属性 int main(int argc, const char * argv[]) { @autorel ...

  5. C# foreach 循环遍历数组

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Cons ...

  6. golang matrix

    package main import ( "fmt" "go.matrix-go1" //比较有名的关于Matrix在golang中的方法库 "st ...

  7. SQL server 错误代码对比表

    0  操作成功完毕.    1  功能错误.    2  系统找不到指定的文件.    3  系统找不到指定的路径.    4  系统无法打开文件.    5  拒绝訪问.    6  句柄无效.   ...

  8. logback--How do I configure an AsyncAppender with code? 转载

    原文地址:https://github.com/tony19/logback-android/issues/54 Please provide an example of how to configu ...

  9. 洛谷—— P1765 手机_NOI导刊2010普及(10)

    https://www.luogu.org/problem/show?pid=1765#sub 题目描述 一般的手机的键盘是这样的: 1 2 abc 3 def 4 ghi 5 jkl 6 mno 7 ...

  10. TransE论文剩余部分

    4.3链接预測 表3:链接预測结果.不同方法的性能. 整体结果 表3显示了全部数据集全部方法的比較. 与预期结果一致,经过过滤设置的结果具有较低的平均排名和较高的hits@10,相信在链接预測方面对各 ...