JavaScript语法反应路由

JavaScript语法反应路由,javascript,Javascript,我想在这里输入路由器代码: 遇到了这个语法 const Modal = ({ match, history }) => { const image = IMAGES[parseInt(match.params.id, 10)] if (!image) { return null } const back = (e) => { e.stopPropagation() history.goBack() } return ( <

我想在这里输入路由器代码:

遇到了这个语法

const Modal = ({ match, history }) => {
  const image = IMAGES[parseInt(match.params.id, 10)]
  if (!image) {
    return null
  }
  const back = (e) => {
    e.stopPropagation()
    history.goBack()
  }
  return (
    <div
      onClick={back}
      style={{
        position: 'absolute',
        top: 0,
        left: 0,
        bottom: 0,
        right: 0,
        background: 'rgba(0, 0, 0, 0.15)'
      }}
    >
      <div className='modal' style={{
      position: 'absolute',
        background: '#fff',
        top: 25,
        left: '10%',
        right: '10%',
        padding: 15,
        border: '2px solid #444'
      }}>
        <h1>{image.title}</h1>
        <Image color={image.color} />
        <button type='button' onClick={back}>
          Close
        </button>
      </div>
    </div>
  )
}
const-Modal=({match,history})=>{
const image=IMAGES[parseInt(match.params.id,10)]
如果(!图像){
返回空
}
常数返回=(e)=>{
e、 停止传播()
history.goBack()
}
返回(
{image.title}
接近
)
}

我的问题是,这是什么类型的语法?您如何将其转换为ES6 React。组件类这是一个纯函数。您可以将其视为
React.Component
render
函数

转换它变得很容易,只需将代码粘贴到渲染函数中:

class Modal extends React.Component {
  render() {
    const { match, history } = this.props; // Here you get your props
    const image = IMAGES[parseInt(match.params.id, 10)]
    if (!image) {
      return null
    }
    const back = (e) => {
      e.stopPropagation()
      history.goBack()
    }
    return (
      <div
        onClick={back}
          style={{
          position: 'absolute',
          top: 0,
          left: 0,
          bottom: 0,
          right: 0,
          background: 'rgba(0, 0, 0, 0.15)'
        }}
      >
        <div className='modal' style={{
        position: 'absolute',
          background: '#fff',
          top: 25,
          left: '10%',
          right: '10%',
          padding: 15,
          border: '2px solid #444'
        }}>
          <h1>{image.title}</h1>
          <Image color={image.color} />
          <button type='button' onClick={back}>
            Close
          </button>
        </div>
      </div>
    )
  }
}
类模态扩展React.Component{
render(){
const{match,history}=this.props;//这是你的道具
const image=IMAGES[parseInt(match.params.id,10)]
如果(!图像){
返回空
}
常数返回=(e)=>{
e、 停止传播()
history.goBack()
}
返回(
{image.title}
接近
)
}
}

这是一个纯函数。您可以将其视为
React.Component
render
函数

转换它变得很容易,只需将代码粘贴到渲染函数中:

class Modal extends React.Component {
  render() {
    const { match, history } = this.props; // Here you get your props
    const image = IMAGES[parseInt(match.params.id, 10)]
    if (!image) {
      return null
    }
    const back = (e) => {
      e.stopPropagation()
      history.goBack()
    }
    return (
      <div
        onClick={back}
          style={{
          position: 'absolute',
          top: 0,
          left: 0,
          bottom: 0,
          right: 0,
          background: 'rgba(0, 0, 0, 0.15)'
        }}
      >
        <div className='modal' style={{
        position: 'absolute',
          background: '#fff',
          top: 25,
          left: '10%',
          right: '10%',
          padding: 15,
          border: '2px solid #444'
        }}>
          <h1>{image.title}</h1>
          <Image color={image.color} />
          <button type='button' onClick={back}>
            Close
          </button>
        </div>
      </div>
    )
  }
}
类模态扩展React.Component{
render(){
const{match,history}=this.props;//这是你的道具
const image=IMAGES[parseInt(match.params.id,10)]
如果(!图像){
返回空
}
常数返回=(e)=>{
e、 停止传播()
history.goBack()
}
返回(
{image.title}
接近
)
}
}

它是一个React无状态功能组件(React SFC)。它是一个函数,而不是一个类

基本上,您不需要/不必将其转换为React.Component类,因为这是声明React.Component的有效方法

SFC通常被称为哑组件,因为它只关心传递给它的道具,而不关心其他。它是一个纯粹的组件(它不应该有任何内部状态),也不应该从其生命周期功能中产生任何副作用

React建议,如果您不需要组件的内部状态或生命周期方法,则应将其作为SFC编写

您可以在此处进一步查看:


它是一个React无状态功能组件(React SFC)。它是一个函数,而不是一个类

基本上,您不需要/不必将其转换为React.Component类,因为这是声明React.Component的有效方法

SFC通常被称为哑组件,因为它只关心传递给它的道具,而不关心其他。它是一个纯粹的组件(它不应该有任何内部状态),也不应该从其生命周期功能中产生任何副作用

React建议,如果您不需要组件的内部状态或生命周期方法,则应将其作为SFC编写

您可以在此处进一步查看:


这称为解构分配。请注意,这不是JavaScript;并且可能还应该加上标签(即使您最终询问的现象在JavaScript和JSX中都可用);并且可能还应该附加标签(即使您最终询问的现象在JavaScript和JSX中都可用)。