reactjs - 为什么 React 无状态函数不能返回数组?

标签 reactjs

这是我的无状态函数,它尝试返回元素数组:

function DeliveryPreference (props) {
  return [
      <h4>
        Delivery Preference
        </h4>,
        <div className='yesNoCheckbox'>
          <input type="radio" value={'tube'} onChange={props.onChange} id='tube' checked={props.value === 'tube'}/> <label htmlFor='tube'>Tube</label> <br/>
          <input type="radio" value={'plate'} onChange={props.onChange} id='plate' checked={props.value === 'plate'}/> <label htmlFor='plate'>Plate</label><br/>
        </div>
  ];
}

任何人都可以解释为什么我不能这样做,和/或提供解决方法吗?

非常感谢!

编辑: 这是错误的最小示例:

function App (props) {
    return [
    <div>1</div>,
    <div>2</div>,
    ]
}


ReactDOM.render( 
    <App/>
    , document.getElementById('root')
);

最佳答案

现在您可以返回一个数组,尝试一下 React 16 Beta

关于reactjs - 为什么 React 无状态函数不能返回数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34816032/

相关文章:

javascript - 如何使用 Jest 模拟 axios-hooks 中的 useAxios 钩子(Hook)? (错误: Uncaught [TypeError: undefined is not a function])

javascript - 如何避免渲染元素尽可能简单

reactjs - 带有 Transition 的对话框抛出 JS 警告 "findDOMNode is deprecated in StrictMode"

javascript - React 组件的状态在 render 中的值与 setState 中的值不同

javascript - Alt "connectToStores"在 React/Flux 应用程序中已弃用?

reactjs - 将绝对路径添加到由 "Create React App"创建的应用程序

javascript - 从 PanResponder 内部更新状态

reactjs - 如何使用 react Hook 表单验证密码并确认密码,是的

javascript - JavaScript React Meteor 的动态导入

javascript - React 修改显示的值而不改变输入表单的值