简体   繁体   English

从条件语句渲染多个 React 组件

[英]Render multiple React components from conditional statements

I have a definition object where depending on the 'type' of each form, I render a specific component.我有一个definition对象,根据每个表单的“类型”,我呈现一个特定的组件。 For example:例如:

const definition = 
{
name: "test", 
id: 1,
form: [
  {type: "text"}, 
  {type: "checkbox"}, 
  {type: "radio"}]
};

So to access the form types Im just doing a simple map:所以要访问表单类型,我只是做了一个简单的地图:

const definitionTypes = definitions.form.map ( form => form.type )

I now have an array of all the form type's entries stored on definitionTypes and I have three components that needs to render depending on which keyword is in that definitionTypes array.我现在有一个存储在definitionTypes上的所有表单类型条目的数组,并且我有三个组件需要根据该 definitionTypes 数组中的关键字进行呈现。 So for the “text” I have a component that Im importing called <DefinitionText /> , etc.所以对于“文本”,我有一个名为<DefinitionText />的组件,我导入了它,等等。

Where Im am experiencing problems is figuring out a solution to return multiple components based on the types that are in the definitionTypes array.我遇到问题的地方是根据 definitionTypes 数组中的类型找出返回多个组件的解决方案。 The end goal would be for these 3 components to render from that array:最终目标是让这 3 个组件从该数组呈现:

  // if definitionTypes includes "text" then render...
  <DefinitionText />

  // also if definitionTypes includes "checkbox" then render...
  <DefinitionCheckbox />

  // also if definitionTypes includes "radio" then render...
  <DefinitionRadio />

I've tried using an if/else statement to push each component to a results variable then rendering that but the results variable sends back an error.我尝试使用if/else语句将每个组件推送到results变量,然后呈现该变量,但results变量发回错误。 Any help in getting multiple components to render based off conditions would be a huge help!任何帮助根据条件渲染多个组件都将是一个巨大的帮助!

Having that defintionTypes array, the rest should be kinda straightforward... You could use a switch statement for example.有了那个defintionTypes数组,剩下的就应该有点简单了……例如,您可以使用 switch 语句。 Check out the snippet.查看片段。

 function Radio(){ return <input type='radio'></input> } function Text(){ return <input type='text'></input> } function CheckBox(){ return <input type='checkBox'></input> } function App(){ const definitionTypes = ['text', 'checkBox', 'radio'] return <div> {definitionTypes.map(type => { switch(type){ case 'text': return <Text/> break case 'radio': return <Radio/> break case 'checkBox': return <CheckBox/> break default: break }})} </div> } ReactDOM.render(<App/>, document.getElementById('root'))
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.8.6/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.6/umd/react-dom.production.min.js"></script> <div id='root'></div>

Firstly build an object map of your components:首先构建组件的对象映射:

const Components = {
  foo: Foo,
  bar: Bar
};

Then access using the string for key name:然后使用密钥名称的字符串访问:

const DynamicComponent = Components[componentName];

<DynamicComponent />

Quick example using your map within a component在组件中使用地图的快速示例

{definitions.form.map( form => {
  const DynamicComponent = Components[form.type];
  return <DynamicComponent />
})}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM