5

Here is a function that has two parameters:

  1. The name of the tag that I want to create
  2. An object that has the properties

Using React, I create a component and render that element to DOM. The problem is that I want to add properties to the element, but it does not allow the loop to set properties within element.

var Element = function(element, properties) {
  var newElement = React.createClass({
    render: function() {
      return (
        React.createElement(element, {}, 'react reuseable')
      );
    }
  });

  ReactDOM.render(React.createElement(newElement, null), document.getElementById('content'));
}

Here is the function call to create a React element:

Element('g', {
  id: 'DrawingController.drawingPathCounter ' + '_shape',
  d: 'path',
  fill: 'none',
  stroke: 'Controllers.TemplateController.wireFrameColour_Selected',
  'stroke-width': 1,
  'class': 'drawingpath',
  pathhover: '',
  'vector-effect': 'non-scaling-stroke'
})

1 Answer 1

3

You're re-implementing the existing React.createElement method.

You can just store the unique props for your components in an array, then create a list of components using those props.

var propsList = [
  { id: 1, d: 'path', fill: 'none' }, 
  { id: 2, d: 'path', fill: 'none' }, 
  { id: 3, d: 'path', fill: 'none' } 
];

var components = propsList.map(function(props) {
  return React.createElement('g', props);
});

var App = React.createClass({
  render: function() {
    return React.createElement('div', null, components);
  }
});

ReactDOM.render(
  React.createElement(App, null),
  document.getElementById('content')
);

If you want the properties list to be dynamic, then you should store it inside your component's state.

var App = React.createClass({
  getInitialState: function() {
    return {
      propsList: []
    };
  },
  addProps: function(props) {
    var propsList = this.state.propsList.concat([props]);
    this.setState({ propsList: propsList });
  },
  render: function() {
    var components = this.state.propsList.map(function(props) {
      return React.createElement('g', props);
    });

    return React.createElement('div', null, components);
  }
});
Sign up to request clarification or add additional context in comments.

7 Comments

i want to make different component with different properties so i this is not satisfies my need i want some modification
The second argument to React.createElement is a properties object, just pass a different properties object in each time you want to make a component with different properties.
will you please do a favor by guide me how i do in this case
this is the code duplication method we are writing the same code again and again just changing the props and this eleminate the concept of modularity. we want to make code modular
But that is the minimum amount of code that it's possible to write. To instantiate a component, the essential bits of data are the name of the component and the props it takes. This is the form that JSX compiles to.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.