score:1
you can put the defaultprop
on the class outside of the constructor like this:
class mycomponent extends component {
constructor(props) {
console.log("props", props);
super(props);
}
render() {
return <div> test </div>;
}
}
mycomponent.defaultprops = {
__type: "mydaterange"
};
alternatively, you can have defaultprops
be a static property on the class:
class mycomponent extends component {
static defaultprops = {
__type: "mydaterange"
};
constructor(props) {
console.log("props", props);
super(props);
}
render() {
return <div> test </div>;
}
}
score:2
i tried and this is working :
import react from "react";
import { render } from "react-dom";
class mycomponent extends react.component {
constructor(props) {
console.log("props", props);
super(props);
}
render() {
return null;
}
}
mycomponent.defaultprops = {
__type: "mydaterange"
};
render(
<mycomponent onenterkey={() => console.log("snuh")} />,
document.getelementbyid("app")
);
you can see it here : https://codesandbox.io/s/wkw0k0j5o8
Source: stackoverflow.com
Related Query
- React - Pass function to component that has default props
- Component not updating when I change the props that I pass to it in React
- How to pass props to react component that wrapped in variable
- How to pass additional props throughout a Route element that has parameters in a Typescript based React Web Application
- How to pass dynamic props to a React component that accepts a union of objects as its type?
- How do I test a React component that calls a function on its parent, which changes props on the component?
- Pass a default prop inside function component in react js
- Unable to pass function in props to a Component | React
- React - how to pass component and props to children component and make that component use that props
- How do you pass a function through a component that has a spread operator?
- React pass the props of a component as a function variable
- React - Create connected provider arround default provider, that will take data from store and pass it as props to default provider
- How to access React router history object with component interface that has default custom hook specified
- Pass React route component to parent component props function
- How to pass a json array that comes from backend to child component using props and react hooks?
- How can I write a react component that has dependent props in TypeScript?
- Pass prop that has a dynamic name in react to child component
- Animating out a React component before unmounting that has lost the props data it needs to display?
- Trying to pass value to a function that has been passed as props
- how do I pass additional props into a react component through a higher order function
- how to pass props from one component to other in react router that don't have parent child relationship?
- Pass const present in other function as a props | React class component
- Pass a function to a component arrived as '' Props ''. - React - Material UI
- React you cannot pass props inside a regular function but you can pass a prop through a class component
- How React HOC function knows the props of a Component that put into it as a parameter
- How can I pass default props as function in functional component
- Pass component via props in react that matches type
- How can i pass props from a functional button component to another function component that displays the prop?
- Pass react component as props
- How to set component default props on React component
More Query from same tag
- How to correctly make side effects during rendering on functional components?
- onClick() works on the first click only - React
- In React useEffect does not update after the value has changed from other component
- 'prop' is missing in props validation?
- Unknown props - react and material-ui
- Jest testing onClick() gets error 'Expected mock function to have been called, but it was not called.'
- How to highlight React tsx with PrismJS
- How to change icon in button in ReactJS?
- How to solve Command "yarn run build" exited with 1 Error!?
- Loop an array get undefined
- How do I create a custom webpack loader that still compiles my react code properly?
- REACT: Can't set useState to an array of objects. Console log shows undefined
- react-router browserHistory.listen unlisten
- Arguments before a colon
- How to arrange values of Object with uniqueID
- React-Router, required prop `to` was not specified in `Link`
- React passing props to children while overwriting parent's parent props
- MUI v5 + styled() + ListItemButton: property 'to'/'component' does not exist
- how to configure dynamic form fields inside a wizard when using a state machine
- Dynamic img src
- Offline Pages with Service-Worker React
- Managing opening and closing of reusable Modal in React+Redux
- How to declare two event types in typescript
- How to set individual states to render components inside a map function in ReactJs
- React) State in useState can't receive value
- Dispatch an action from container
- Reuseable api hook with type support
- How to get the current zoom of the map [getZoom()] in react-google-maps?
- ReactJS component loses input values entered by the user
- How can I access key using value in ReactJs?