score:5
Accepted answer
i think, there are two solutions possible:
1- use spread syntax ...
, and pass put all the passed values on button, by this was you don't need define each property separately. like this:
return <button
key={index}
classname={`btn btn-default ${element.class} ${element.secondary ? "btn-secondary" : ""}`}
type="button"
{...element}
>
{element.label}
</button>
now if you pass onclick then only it will be assigned otherwise not.
2- use destructuring and assign the default value if not passed from the parent.
like this:
{this.props.datasrc.map((element, index) => {
const {onclick=null, datagrid=''} = element;
return <button
key={index}
classname={`btn btn-default ${element.class} ${element.secondary ? "btn-secondary" : ""}`}
type="button"
disabled={element.disabled}
onclick={onclick}
data-grid={datagrid}
>
{element.label}
</button>
})}
Source: stackoverflow.com
Related Query
- Add dynamic attribute in ReactJS
- Reactjs add onClick attribute on a dynamic element
- Dynamic attribute in ReactJS
- How to add "colSpan" attribute in ReactJS
- unable add required attribute to a ReactJS input component
- Reactjs Add dynamic component into other component
- Add dynamic tag using reactjs
- Add select attribute conditionally in ReactJS
- conditional add JSX attribute in ReactJS
- ReactJS how to add momentJS to object attribute
- Add attribute in child element of dynamic HTML
- Add CSS style using className in Material UI component coming from dynamic json object in ReactJS
- Dynamic Add / Remove of Dropdown in ReactJS
- How to properly do a dynamic route in reactJs when working with Add to Cart ecommerce functionality
- How to add dynamic multiple text field in reactjs
- ReactJS Javascript - Dynamic Add / Remove Rows of Input in Form
- How to add multiple classes to a ReactJS Component?
- Reactjs setState() with a dynamic key name?
- ReactJS - Add custom event listener to component
- How to add a <br> tag in reactjs between two strings?
- How to add delay in Reactjs
- How to add a dynamic class to body tag in Gatsby.js?
- What exactly is Dynamic Routing in ReactJS
- Load images based on dynamic path in ReactJs
- Dynamic tabIndex attribute in JSX + React
- Reactjs - Adding ref to input in dynamic element render
- How to create a dynamic Link to attribute in React-Router 2.5
- ReactJS not fetching data from server when using 'url' attribute
- how to add row to a table using ReactJS on button click
- How to add external javascript file with reactjs
More Query from same tag
- How do I Link Components correctly using React Router?
- range slider with reactjs - value changing but slider not moving
- PropType validations for multiple react components in a single file
- Hide react-tooltip after certain time
- React - recomputing values in children components
- ReactJS/JestJS/Enzyme: How to test graphql() function used in compose()?
- Unable to display generated HTML ,from Showdown plugin, in React component
- popover is showing at top left corner as the width of the div is very small like < 2.5 px
- How to get data into PrimeReact Table
- How to resolve `require` aliases built with Webpack, for React Unit tests?
- Module not found: Can't resolve '@material-ui/core/Avatar' in 'D:\own_files\ReactTutorial\react-app\src'
- How concatenate string with value in recharts - ReactJS
- How to find the values form array of objects
- Push JSX element as a child of another JSX element inside an Array (ReactJS)
- How to make shaking image animation in React Native?
- Accessing data from local file by id (from params, React Router v6)
- How to pass props with React/Typescript?
- How to load es6, react, babel code in html with cdn?
- How to get type of first argument of function?
- React Transition Group prevent transition on param change
- I can't seem to access the geolocation return value
- How can I use variables inside window before unload
- Sharing data between React components
- How to correctly change my state in my reducer
- How to test trigger on change event by selecting data list
- How to overwrite styles with classes and css modules?
- React fetch posting multiple values under same array
- Modal keeps triggering when I press a key
- How can I format this gatsby-config.js file in order to create a dummy node for a canvas module?
- React useState and map()