How to pass Parameters with require tag in React

2.1k Views Asked by At

I am implementing a dynamic rendering in React in that I am using syntax like

 var Set=require('../../'+RecivedData.Id);
 React.render(<CustomComponent inputType={Set} />,document.getElementById("secondCol"));

and my CustomComponent looks like

render: function() {
        var InputType=this.props.inputType;
        console.log(InputType)
        return (

            <InputType />

         );
    },

I just want to know how I can pass the parameters, I mean to say if we use in static mode then we can use below mentioned syntax

var Item=require('../../Item');
  React.render(<Item MetaItemName={"Users"} MetaItemId={1} Key={1}/>,document.getElementById("firstCol")); 

Same Parameters I have to pass when I am using require dynamic rendering.

so can anyone please let me know the syntax to pass the parameters while using require

1

There are 1 best solutions below

0
On BEST ANSWER

You are using CommonJS, which unlike AMD can't be dynamically typed in the browser. This is because the dependencies need to be built in at compilation time, and the paths are relative to where they are required. You will have to do something like this:

var itemList: {
  item1: require('components/items/item1'),
  item2: require('components/items/item2')
};

Then:

var Item = itemList['item' + ReceivedData.Id];
React.render(<Item MetaItemName={'Users'} ... />, document.getElementById('secondCol'));