score:151
you forgot to close the class
bracket.
class addaddresscomponent extends react.component {
render() {
let {provincelist,citylist} = this.props
if(citylist === undefined || provincelist === undefined){
console.log('undefined props')
} else {
console.log('defined props')
}
return (
<div>rendered</div>
)
}
}
addaddresscomponent.contexttypes = {
router: react.proptypes.object.isrequired
};
addaddresscomponent.defaultprops = {
citylist: [],
provincelist: [],
};
addaddresscomponent.proptypes = {
userinfo: react.proptypes.object,
citylist: react.proptypes.array.isrequired,
provincelist: react.proptypes.array.isrequired,
}
reactdom.render(
<addaddresscomponent />,
document.getelementbyid('app')
)
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id="app" />
score:1
class example extends react.component {
render() {
return <h1>{this.props.text}</h1>;
}
}
example.defaultprops = { text: 'yo' };
score:4
for a function type prop you can use the following code:
addaddresscomponent.defaultprops = {
callbackhandler: () => {}
};
addaddresscomponent.proptypes = {
callbackhandler: proptypes.func,
};
score:5
use a static defaultprops like:
export default class addaddresscomponent extends component {
static defaultprops = {
provincelist: [],
citylist: []
}
render() {
let {provincelist,citylist} = this.props
if(citylist === undefined || provincelist === undefined){
console.log('undefined props')
}
...
}
addaddresscomponent.contexttypes = {
router: react.proptypes.object.isrequired
}
addaddresscomponent.defaultprops = {
citylist: [],
provincelist: [],
}
addaddresscomponent.proptypes = {
userinfo: react.proptypes.object,
citylist: proptypes.array.isrequired,
provincelist: proptypes.array.isrequired,
}
taken from: https://github.com/facebook/react-native/issues/1772
if you wish to check the types, see how to use proptypes in treyhakanson's or ilan hasanov's answer, or review the many answers in the above link.
score:5
you can set the default props using the class name as shown below.
class greeting extends react.component {
render() {
return (
<h1>hello, {this.props.name}</h1>
);
}
}
// specifies the default values for props:
greeting.defaultprops = {
name: 'stranger'
};
you can use the react's recommended way from this link for more info
score:8
if you're using a functional component, you can define defaults in the destructuring assignment, like so:
export default ({ children, id="menu", side="left", image={menu} }) => {
...
};
score:10
you can also use destructuring assignment.
class addaddresscomponent extends react.component {
render() {
const {
province="insertdefaultvaluehere1",
city="insertdefaultvaluehere2"
} = this.props
return (
<div>{province}</div>
<div>{city}</div>
)
}
}
i like this approach as you don't need to write much code.
score:16
first you need to separate your class from the further extensions ex you cannot extend addaddresscomponent.defaultprops
within the class
instead move it outside.
i will also recommend you to read about the constructor and react's lifecycle: see component specs and lifecycle
here is what you want:
import proptypes from 'prop-types';
class addaddresscomponent extends react.component {
render() {
let { provincelist, citylist } = this.props;
if(citylist === undefined || provincelist === undefined){
console.log('undefined props');
}
}
}
addaddresscomponent.contexttypes = {
router: proptypes.object.isrequired
};
addaddresscomponent.defaultprops = {
citylist: [],
provincelist: [],
};
addaddresscomponent.proptypes = {
userinfo: proptypes.object,
citylist: proptypes.array.isrequired,
provincelist: proptypes.array.isrequired,
}
export default addaddresscomponent;
score:96
for those using something like babel stage-2 or transform-class-properties:
import react, { proptypes, component } from 'react';
export default class examplecomponent extends component {
static contexttypes = {
// some context types
};
static proptypes = {
prop1: proptypes.object
};
static defaultprops = {
prop1: { foobar: 'foobar' }
};
...
}
update
as of react v15.5, proptypes
was moved out of the main react package (link):
import proptypes from 'prop-types';
edit
as pointed out by @johndodo, static
class properties are actually not a part of the es7 spec, but rather are currently only supported by babel. updated to reflect that.
Source: stackoverflow.com
Related Query
- How to set component default props on React component
- How to declare default props in react functional component
- How to set React component state and props from browser
- How to set React default props for object type
- How to test default props functions is attached to component or not ? | React | Jest | Enzyme
- Set default props for a react Component
- How to set default value of props if not passed to functional component in NextJS using Typescript
- How to set the default style to a React Component
- How can I set default values for props on a React TypeScript component?
- How do I set default props for a component wrapped in a Raley container?
- How do I use default props in a React component with Typescript?
- How to set dynamic attributes in React Input Component from json props
- How to set color of existing css class, from props of a react component
- React functional component default props vs default parameters
- How to specify (optional) default props with TypeScript for stateless, functional React components?
- How to set iframe content of a react component
- React - How to pass props to a component passed as prop
- How to optimize small updates to props of nested component in React + Redux?
- How to pass props to component inside a React Navigation navigator?
- React JSX: How to set props to placeholder attribute
- How to initialize the react functional component state from props
- How to set the width of a React component during test?
- How to extend a native's component's props TypeScript interface in a stateless, functional component in React Native?
- How to set -webkit-overflow-scrolling inline style on react component
- How to TEST async calls made in componentDidMount that set the state of React Component
- React Typescript how send props and use it in child component
- How to update props of React Component rendered using ReactDOM.render()
- How to pass props to react component that wrapped in variable
- How to recursively inject props to each React component in a tree?
- How to find out which react component throws empty props error
More Query from same tag
- How I can pass attributes directly to component in vuejs, like reactjs
- Objects are not valid as a React child error in React Datatable
- ESLint warns React Query mutation must be included in dependency array
- Map function in react (err: TypeError: e.map is not a function)
- How to make div's inline in particular screen by using media queries
- Kick off separate redux-saga on login and logout
- Too many re-renders - while trying to put props(if exists) in state
- How to set state of an Array of objects using useState?
- How can I render my component X amount of times based on a selector?
- NextJS using React's Context API with a layout component wrapper
- ReactJS doesn't recognize a value in an object in an array
- React and Validation library
- React Router v4 Navigate from MobX store action?
- React.js. How to pass local images dynamicly according to id property?
- docker-compose up exit with code 0 for react js
- Context & Reducer not returning State
- React TypeScript: useState never updates
- Cannot read property 'value' of null in ReactJS
- Send both 404 status and redirect to 404 component in react-router
- Special characters in hasura graphql queries
- How to update useState() when value changes in React
- How can one set multiple authorization headers in axios?
- this.props.history.push() skips middle page
- Create React App - (ESLint) Failed to load config "shared-config" to extend from. Referenced from: <PATH> in Visual Studio
- How to create a d3 force layout graph using React
- Passing Props between components
- How do array equals useState declarations work in Javascript?
- Avoiding state mutation in a recursive function (Redux)
- react redux firestore mapStateToProps issue while getting data
- ReactJs - Link dropdown should open separately while hovering