Here is a function that has two parameters:
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'
})
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);
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With