簡體   English   中英

如何在jsx中顯示頁面狀態?

[英]How to display state on page in jsx?

我剛剛開始查看react / redux,這是我的組件:

const store = createStore(reducer);

const rootEl = document.getElementById('root')

//render root component
const render = () => ReactDOM.render(

  {store.getState()}


  <List

    addToList={() => store.dispatch(
      {
        type: 'ADDTEXT',
        payload: {
          text: 'this is a text'
        }
      }
    )}
      />

  ,
      rootEl
      )

//call
      render()

//subscribe the store
      store.subscribe(render)

我以為我可以將js與html混合使用,但是store.getState()給我這個錯誤:

Syntax error: Unexpected token (22:8)

  20 | const render = () => ReactDOM.render(
  21 | 
> 22 |   {store.getState()}

如何在UI上顯示狀態?

這里的問題很簡單。 JSX不支持返回多個JSX節點。 在這里閱讀更多。

因此,將所有這些放在一個div中,它將起作用:)

<div>
  {store.getState()}
  <List addToList={() => store.dispatch({
      type: 'ADDTEXT',
      payload: {
        text: 'this is a text'
      }
    })
  }/>
</div>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM