- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在无状态函数组件中,我们可以使用解构的 props
作为函数参数:
const Blah = ({ hello, world }) {
return <div>{hello} {world}</div>
}
这提供了非常干净的代码,不仅很明显正在传递什么 Prop ,而且我们不必到处使用 this.props
。
必须在 class
组件中始终使用 this.props
可能会占用整个组件的大量空间,并且使用起来不太好:
class Blah extends Component {
render() {
return (
<div>{this.props.hello} {this.props.world}</div>
)
}
}
我的问题是,我们可以对 class
组件采取什么类似的方法,这样我们就不必到处使用 this.props
?
我能想到的一个解决方案是解构 render
方法中的 props
,但是当然,您必须对 中的每个方法都执行此操作>class
如果您想使用解构名称。
最佳答案
不,恐怕没有办法。功能组件本质上是类组件的 render()
函数,“清洁度”实际上有点相同(:
关于reactjs - 无法引用类组件中立即解构的 props,我每次都必须使用 `this.props` 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43676833/
我在寻找有关 JavaScript 解构的信息,并在 Packt Publication 的视频系列中找到了视频“解构赋值”。在视频的一开始,我看到了以下代码: var [a, b] = [1,2,3
知识渊博的人可以帮助我理解 Scalaz 的工作原理吗?和 co.有效吗?我是 Scalaz 的新手,有点迷失在探索中。 我想做的是在 List 中累积错误,例如 (v0 v1) foldLeft
Haskell 中的模式匹配可以用这种方式解构数字吗: f (n + 1) = n 我期望 ex 的前身:f 6 = 5、f 5 = 4 等。 我在这里找到了这种模式匹配用法: https://wik
我有一个与此类似的文件: const COLORS = { PRIMARY_COLOR: 'red', SECONDARY_COLOR: 'green' }; const APP = {
作为对 SO 问题的回答,我构建了一个循环函数,并构建了我迄今为止最复杂的解构,这奇迹般地起作用了: (defn fib? [a b & [c & r]] (if (= c (+ a b))
我对破坏感到困惑。 我在其中使用了 React,我们这样做了 const [ books, setBooks ] = useState([{a:'v'}] 和 const {books} = useC
我确定这在某个地方得到了回答,但我缺乏制定搜索的词汇。 #include class Thing { public: int value; Thing(); virtual ~Thing()
如果我在 javascript 中有这样一个对象: let obj = {b: 3, c: 4, d: 6} 如果我解构它,我可以很容易地得到不同的部分,例如,如果我这样做,我可以得到 c 和 d:
这个问题在这里已经有了答案: One-liner to take some properties from object in ES 6 (11 个回答) 2年前关闭。 我似乎不记得如何写这个解构模式
我有一个函数可以查询我的数据库中最近的 X 个条目,它返回一个 map 向量,如下所示: [{:itemID "item1" :category "stuff" :price 5} {:itemI
根据所选语言,我需要销毁对象并获得所需的值。 我该怎么做才能不破坏整个对象? const translate = { "navMenu1": { "en": "Menu 1",
我试图理解这种 ES6 解构。有人可以解释这行代码将编译成什么吗? const { loading, route: { pageName = 'default' } = {} } = this.pro
我有一个程序,可以输出这样的一些条件(这是实际输出,它是伪代码): if ( first occurance of 'AB' -0.5 ) * (( number of products viewe
Serilog的@的目的是什么?句法? 如果我运行以下命令: var dummy = new { Foo = "Bar", Date = DateTime.Now }; Log.Information
JSON 编码的数组从 PHP 传递到 HTML 文档。目前还不清楚如何将该数组解构为 javascript 可用的片段。例如,考虑以下 HTML: {"foo":[{"id":1},{"id":3}
我正在 Chrome 的控制台选项卡中尝试使用以下代码进行 JavaScript 解构,这给了我未捕获的语法错误:标识符“a”已被声明异常 o = { a: "foo", b: 12, c: "bar
我有一个 JavaSCript 对象 person,其中包含 id、name、phone 和 地址属性。我想修改属性名称并将它们放入新对象 personData 中。这可以一步完成吗?: 第 1 步(
有没有办法从 WPF 中获取 Geometry 实例的内部结构? 我需要转换一串用户输入的几何数据,例如 M10,100 C10,300 300,-200 300,100 Z 用于分离几何命令(移动、
我的代码中有一个 promise : req.getValidationResult() .then(result => { let errors = resu
我正在为 Apollo Client 生成 Flow 类型,我目前有这个: type FetchModuleQuery = {| // Fetch single module module:
我是一名优秀的程序员,十分优秀!