第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

React/Gatsby 組件交互(提升狀態(tài))與 MDX 文件中的組件

React/Gatsby 組件交互(提升狀態(tài))與 MDX 文件中的組件

繁華開滿天機 2022-12-18 18:58:50
我將 Gatsby 與 MDX 插件一起使用。所以我可以在 markdown 中使用 React 組件。沒關(guān)系。我有組件,互相交談。為此,我使用提升狀態(tài)向上模式。沒關(guān)系。這是一個基本的 Counter 示例,用于展示我的概念驗證代碼。import React from "react"export class Counter extends React.Component {  constructor(props) {    super(props)    this.state = { count: 0 }    this.handleCounterUpdate = this.handleCounterUpdate.bind(this)  }  handleCounterUpdate() {    this.setState({ count: this.state.count + 1 })  }  render() {    const children = React.Children.map(this.props.children, child => {      const additionalProps = {}      additionalProps.count = this.state.count      additionalProps.handleCounterUpdate = this.handleCounterUpdate      return React.cloneElement(child, additionalProps)    })    return <div>{children}</div>  }}export function Display({ count }) {  return <h2>Current counter is: {count}</h2>}export function UpdateButton({ handleCounterUpdate }) {  return <button onClick={handleCounterUpdate}>Increment couter by one</button>}通過此設(shè)置,可以使用這樣的組件<Counter>  <Display />  <UpdateButton /></Counter>甚至像這樣<Counter>  <Display />  <UpdateButton />  <Display />  <Display /></Counter>沒關(guān)系。在現(xiàn)實世界中,封閉的 Counter 組件(狀態(tài)持有者)將類似于 Layout 組件。在<Layout>模板中使用并呈現(xiàn) MDX 頁面。這看起來像這樣:<SiteLayout>  <SEO title={title} description={description} />  <TopNavigation />  <Display />       // The state holder is <SiteLayout>, not <Counter>   <Breadcrumb location={location} />  <MDXRenderer>{page.body}</MDXRenderer>  // The rendered MDX</SiteLayout>(<UpdateButton>在現(xiàn)實世界中類似<AddToCartButton>)在 MDX 頁面上,不再是<Layout>組件的直接子項。該模式不再起作用。我該如何解決這個問題?
查看完整描述

1 回答

?
瀟瀟雨雨

TA貢獻1833條經(jīng)驗 獲得超4個贊

import React from "react"


// This is a proof of concept (POC) for intercomponent communication using

// React Context

//

// In the real world Gatsby/React app we use a kind of cart summary component

// at top right of each page. The site consists of thousands of pages with detailed

// product information and a blog. Users have the possibility to add products directly

// from product information pages and blog posts. Posts and pages are written in

// MDX (Mardown + React components). All <AddToCartButtons> reside in MDX files.

//

// This code uses a "increment counter button" (= add to cart button) and a

// display (= cart summary) as POC

//

// More information at

// https://reactjs.org/docs/context.html#updating-context-from-a-nested-component


export const CounterContext = React.createContext()


// The <Layout> component handles all business logic. Thats fine. We have

// very few app features.

export class Layout extends React.Component {

  constructor(props) {

    super(props)

    this.handleCounterUpdate = this.handleCounterUpdate.bind(this)

    this.state = { count: 0, increment: this.handleCounterUpdate }

  }


  handleCounterUpdate() {

    this.setState({ count: this.state.count + 1 })

  }

  render() {

    return (

      <div style={{ backgroundColor: "silver", padding: "20px" }}>

        <CounterContext.Provider value={this.state}>

          {this.props.children}

        </CounterContext.Provider>

      </div>

    )

  }

}


export class UpdateButton extends React.Component {

  static contextType = CounterContext

  render() {

    const count = this.context.count

    const increment = this.context.increment

    return (

      <button onClick={increment}>

        Increment counter (current value: {count})

      </button>

    )

  }

}


export class Display extends React.Component {

  static contextType = CounterContext


  render() {

    return (

      <div

        style={{

          backgroundColor: "white",

          padding: "10px",

          margin: "10px 0 0 0"

        }}

      >

        <div>I'm Display. I know the count: {this.context.count}</div>

        <div>{this.props.children}</div>

      </div>

    )

  }

}


// Function components use <CounterContext.Consumer>. Class components

// use static contextType = CounterContext

export function AChild() {

  return (

    <CounterContext.Consumer>

      {context => (

        <span>

          I'm a child of Display. I know the count too: {context.count}

        </span>

      )}

    </CounterContext.Consumer>

  )

}

像這樣使用


<Layout>

  <Display>

    <AChild />

  </Display>

  // UpdateButton inside MDX files

  <MDXRenderer>{page.body}</MDXRenderer>

 // Or elsewhere

  <UpdateButton /> 

</Layout>


查看完整回答
反對 回復(fù) 2022-12-18
  • 1 回答
  • 0 關(guān)注
  • 132 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號