小编Fra*_*cis的帖子

为什么使用 React Context 比仅仅传递 props 对象更好?

我一直在阅读有关在 React 中使用 Context 的优点的文章,但我并不相信。我想知道是否有什么我错过了。

Context 提供了一种通过组件树传递数据的方法,而无需在每个级别手动向下传递 props。

在主组件中创建 props 对象并在下属之间传递它有什么麻烦?就像是:

// do this once at top level (I'm assuming [foo, foo_set] and [bar, bar_set] are state variables):
const props = {foo, foo_set, bar, bar_set, thisAndThat, theOther, whatever, etcAndEtc}

// including one component
<MyComponent1 {...props } />

// including another
<MyComponent2 {...props } />
Run Code Online (Sandbox Code Playgroud)

(也许对该对象使用另一个名称而不是 props 更好,因为组件可以具有其他属性。无论如何。)

然后在 MyComponent1 中你可以访问所有你想要的 props,也可以不访问它们。任何一个:

...
const MyComponent1 = (props) => {
...
// here we can use any props we need, props.bar, props.bar_set, …
Run Code Online (Sandbox Code Playgroud)

reactjs react-context

3
推荐指数
1
解决办法
3125
查看次数

没有箭头反应javascript箭头功能?

我不明白这在javascript中是如何工作的

renderMarkButton(type, icon) {
Run Code Online (Sandbox Code Playgroud)

它看起来像一个箭头功能,但没有箭头.这是上下文:

class HoverMenu extends React.Component {

  renderMarkButton(type, icon) {
    const { editor } = this.props
    return (
      <div className="editorButton" 
            onMouseDown={event => this.onClickMark(event, type)}>
        <FontAwesomeIcon color="#666" active={isActive} 
            className="editorButton" icon={icon}  />
        </div>
    )
  }
  render() {
    return (
      <div>
        {this.renderMarkButton('bold', {...faBold})}
      </div>
    )
  }
}
Run Code Online (Sandbox Code Playgroud)

我也很困惑

  const { editor } = this.props
Run Code Online (Sandbox Code Playgroud)

我相信它来自Slate.我希望this.props在这种情况下是{type,icon}.

javascript reactjs arrow-functions slate

1
推荐指数
1
解决办法
129
查看次数