ES6 μ§μ λ°ν λ λ€μκ³Ό κ°μ΄ λ§ν©λλ€.
ν΄λμ€ μνλ₯Ό μ§μ νλ κ΄μ©μ λ°©λ²μ κ°λ¨ν μΈμ€ν΄μ€ μμ±μ μ¬μ©νλ κ²μ λλ€. λ§μ°¬κ°μ§λ‘ getDefaultProps λ° propTypesλ μ€μ λ‘ μμ±μμ μμ±μΌ λΏμ λλ€.
μ΄κ²μ λμκ² λ§μ μλ―Έκ° μμ§λ§ λ€μ μκ°ν κ°μΉκ° μλ λͺ κ°μ§ μμ λΆμΌμΉλ₯Ό λ°κ²¬νμ΅λλ€.
μλ .createClass
ꡬ문μ μ¬μ©ν λ getDefaultProps
λ°νλ κ°μ μμ±μλΏλ§ μλλΌ κ΅¬μ± μμ μλͺ
μ£ΌκΈ°μ λ€λ₯Έ μ§μ μμ μ¬μ©λλ κ² κ°μ΅λλ€. μλ₯Ό λ€μ΄ componentWillReceiveProps(props)
μ μ‘λλ λ΄μ©μ κ²μ¬νλ©΄ κΈ°λ³Έ propsκ° μ μ©λλ κ²μ λ³Ό μ μμ΅λλ€.
μ΄κ²μ ES6 ν΄λμ€ κ΅¬λ¬Έμ μ¬μ©ν λ _νμ§ μλ_ κ² κ°μ΅λλ€. μ¦, μ½λλ₯Ό 볡μ ν΄μΌ ν©λλ€. λ΄κ° μλ―Ένλ λ°μ μλ λ€μκ³Ό κ°μ΅λλ€.
class Control extends React.Component {
constructor(props) {
props.value = props.value || '';
super(props);
}
// ...
componentWillReceiveProps(props) {
props.value = props.value || '';
// Do something with props...
}
}
보μλ€μνΌ props.value = props.value || ''
ννμμ 볡μ νκ³ μμ΅λλ€. κΈ°λ³Έκ°μ΄ λ κ° μ΄μμ΄μλ€λ©΄ λΆλͺ
ν λ λ§μ μ€λ³΅μ΄ μκ²Όμ κ²μ
λλ€.
μ¬μ©νλ κ²½μ° .createClass
λ°©λ²μ, λλ λ°ν ν μ {value: ''}
λ‘λΆν° getDefaultProps
λ°©λ²μ,μ΄ μλ κ², λ΄κ° ν λ²λ§ν΄μΌ ν κ²μ
λλ€.
λΆνμν μ€λ³΅μ νΌνκΈ° μν΄ μ΄ λ°©λ²μ 볡μνλ κ²μ΄ ν©λ¦¬μ μ λκΉ? λ΄κ° λͺ¨λ₯΄λ λ λ€λ₯Έ Reactμ μ μ¬ν μ κ·Ό λ°©μμ΄ μμ΅λκΉ?
componentWillReceivePropsκ° νΈμΆλ λ κΈ°λ³Έ propsλ μ΄λ―Έ λ³ν©λμ΄ μμ΄μΌ ν©λλ€.
componentWillReceivePropsκ° νΈμΆλ λ κΈ°λ³Έ propsλ μ΄λ―Έ λ³ν©λμ΄ μμ΄μΌ ν©λλ€.
λ΄κ° λ§ν μ μλ ν getDefaultProps
λ ES6 ν΄λμ€ κ΅¬λ¬Έμ μ¬μ©ν λ νΈμΆλμ§ μμ΅λλ€.
λ§μ΅λλ€. λμ λ€μκ³Ό κ°μ΄ μμ±ν©λλ€.
class Control extends React.Component {
// ...
componentWillReceiveProps(props) {
// Do something with props...
}
}
Control.defaultProps = {value: ''};
κ·Έκ² λΉμ μ μν΄ μλν©λκΉ?
μ, μ£μ‘ν©λλ€. μ§κΈ λ¬Έμμμ νμΈνμ΅λλ€. κ°μ¬ ν΄μ!
Google κ²μμ ν΅ν΄ μ¬κΈ°μ λμ°©νκ³ μ½λλ₯Ό μΊ‘μννλ λ° class
λ₯Ό μ νΈνκΈ° λλ¬Έμ κΈ°λ³Έ μνμ λ€μκ³Ό κ°μ΄ κ³μ°λ μμ±μ μ¬μ©νμ¬ μ€μ ν μ μμ΅λλ€.
import React, {Component, PropTypes} from 'react'
class DefaultPropsExample extends Component {
static defaultProps = {
...Component.defaultProps,
instructions: 'Usage instructions not provided.',
}
}
@jhabdas λΉμ μ΄ μ€ κ²μ ES2015 κ΅¬λ¬Έμ΄ μλλλ€. λ΄κ° static propsλ₯Ό μ¬μ©νλ©΄ Babelμμ μκΈ°μΉ μμ ν ν°μ΄ λ°μνμ§λ§ es2015μμ λμ κ°μ μμ μ νλ μ¬λλ€μ μν΄ μλνλ νλͺ©μ΄ μμ΅λλ€.
import React from 'react';
export default class extends React.Component {
static get defaultProps() {
return {
// stuff you want :)
}
}
}
μ λ°μ΄νΈ
@zpao μ λ§ν¬λ₯Ό μ€μΉν ν μ μ μμ±μ μ¬μ©ν μ μμ΅λλ€ ...
@Gopikrishna19 ν΄λμ€ μμ± μ μμ ꡬννλ λ€λ₯Έ babel νλ¬κ·ΈμΈμ νμ±νν΄μΌ ν©λλ€. http://babeljs.io/docs/plugins/transform-class-properties/
@zpao μ€μ€ ! μ€λ λ λ€λ₯Έ μλ‘μ΄ κ²λ€ :) κ·Έκ²μ μλν κ²μ λλ€, λλ¨ν κ°μ¬ν©λλ€!
@Gopikrishna19 μ€μ λ‘ κ²μν μ½λ μ‘°κ°μ΄ λ§μμ λλλ€. κΈ°λ³Έ μνμ μ΄κΈ°ννκΈ° μ μ μ¬μ μ²λ¦¬λ₯Ό ν μ μμ΅λλ€. μΈκΈν΄ μ£Όμ μ κ°μ¬ν©λλ€.
μ΄κ²μ μλνλ κ² κ°κ³ λ΄ μ견μΌλ‘λ λ μ°μν©λλ€.
....
static defaultProps = {
//someDefaultProps
};
constructor(props, defaultProps) {
super(props, defaultProps);
}
....
@fxhereng λ λ²μ§Έ μΈμλ context
μ©μΌλ‘ μμ½λμ΄ μμΌλ―λ‘ ν₯ν 릴리μ€μμ μ€λ¨λ μ μμΌλ―λ‘ μΌλΆ μ¬μ©μ μ μ μλ―Έλ‘ μ¬μ μνμ§ μμ κ²μ
λλ€.
Ok @gaearon κΈ°λ³Έ μνμ μ€μ νλ κ°μ₯ μ’μ λ°©λ²μ 무μμ λκΉ?
κ°μ¬ ν΄μ,
μ΄μ λν κΆμ₯ λ°©λ²μ 무μμ λκΉ?
μ€νμ λ³ν http://babeljs.io/docs/plugins/transform-class-properties/ λ₯Ό μ¬μ©νλ κ²½μ° static defaultProps = {...};
λ§ μ¬μ©ν μ μμ΅λλ€. μμ±μλ₯Ό λ³κ²½ν νμκ° μμ΅λλ€. κ·Έλ μ§ μμΌλ©΄ μΈλΆμ ν λΉν΄μΌ ν©λλ€.
class X extends React.Component {
}
X.defaultProps = {...};
λν..
class X extends React.Component {
props = {
...
}
}
@efernandesng μ§μλμ§ μλ ν¨ν΄μ
λλ€. defaultProps
μ²λΌ μλνμ§ μμΌλ©° this.props
λ₯Ό λ³κ²½νμ§ μμμΌ ν©λλ€.
defaultPropsλ₯Ό λΆλͺ¨ ν΄λμ€μ ν¨κ» μΈμ€ν΄μ€ννμ¬ ν΄λΉ μνμ λ€λ₯Έ μμ ν΄λμ€μ μ λ¬ν μ μλ κ² κ°μ΅λλ€.
κ·Έλ κ²νλ νμ€ es6 λ°©λ²μ΄ μμ΅λκΉ? μ¬κΈ°μ μλ λͺ¨λ λ°©λ²μ μ μκ² μ ν©νμ§ μμ΅λλ€.
defaultPropsλ₯Ό λΆλͺ¨ ν΄λμ€μ ν¨κ» μΈμ€ν΄μ€ννμ¬ ν΄λΉ μνμ λ€λ₯Έ μμ ν΄λμ€μ μ λ¬ν μ μλ κ² κ°μ΅λλ€.
λ¬΄μ¨ λ§μμ΄μ μ§ μ΄ν΄κ° λμ§ μμ΅λλ€. μλ₯Ό 보μ¬μ€ μ μμ΅λκΉ?
λ¬Όλ‘ μ£μ‘ν©λλ€.
λ€μμ κ΅¬μ± μμμ λλ€.
import React,
{
Component,
PropTypes
} from 'react';
import { TimerView } from './TimerView'
class Timer extends Component {
constructor(props) {
super(props);
}
componentWillReceiveProps(props) {
console.log('Will receive props')
}
render() {
console.log("Timer loaded")
return (
<TimerView {...props} />
)
}
}
Timer.propTypes = {
status: PropTypes.string.isRequired,
};
Timer.defaultProps = {
status: "This is the Timer",
};
export default Timer;
webpack-dev-serverλ₯Ό μ€ννλ©΄ λ€μ μ€λ₯κ° λ°μν©λλ€.
Uncaught ReferenceError: props is not defined
λ΄κ° λκ° μλͺ»νκ³ μλκ² νλ¦Όμμ΄...
props
μ render()
λ©μλμ λ°μΈλ©λμ§ μμ λ³μμ
λλ€. <TimerView {...this.props} />
μ΄μ΄μΌ ν©λλ€.
μ΄λ¨Έ.
λλ μ΄κ²μ μμ ν λμ³€λ€. λλ μν λΉμ μ₯ κ΅¬μ± μμλ₯Ό ν΄λμ€λ§νΌ λ§μ΄ μ¬μ©νκΈ° λλ¬Έμ μ΄κ²μ λν΄ νΌλμ€λ¬μνμ΅λλ€.
κ°μ¬ν©λλ€.
λ¬Έμ μμ΄μ @jeanmichelcote! κ·Έκ²μ μΌμ΄λλ€ :)
defaultPropsλ₯Ό μ μνκΈ° μν΄ λ€λ₯Έ propsμ μ‘μΈμ€νλ κ²μ μν° ν¨ν΄μΌλ‘ κ°μ£Όλ©λκΉ?
class Comp extends from React.Component {
static propTypes: {
num: React.PropTypes.number.isRequired,
action: React.PropTypes.func,
};
static defaultProps: {
action: () => console.log(this.props.num), // "this" is invalid in static context
};
render() {
return (
<button onClick={this.props.action}>
{`Log #${this.props.num}`}
</button>
);
}
}
@romulof μ μ λ©μλμμ μΈμ€ν΄μ€ λ³μλ₯Ό μ°Έμ‘°νλ €λ κ² κ°μ΅λλ€. μ΄λ€ this
μ λν΄ μ΄μΌκΈ°νκ³ μλμ§ μ μ μλ λ°©λ²μ΄ μκΈ° λλ¬Έμ κ·Έλ κ² ν μ μμ΅λλ€. μ μμ λ°λ₯΄λ©΄ μ μ μ¬λ¬Όμ νμ μΈμ€ν΄μ€ 컨ν
μ€νΈ μΈλΆμ μμ΅λλ€.
@romulof κ·Έκ²μ λͺ¨λ κ°μ²΄ μ§ν₯ μΈμ΄μ 곡ν΅μ μ λλ€. λ±λ±ν νμ΄ν ꡬ문μ μ μλ 컨ν μ€νΈμ λ°μΈλ©λ©λλ€. μλ§λ λ€μμ μ¬μ©ν΄ λ³Ό μ μμ΅λλ€.
static defaultProps: {
action: function() {
console.log(this.props.num), // "this" depends on the context where it is run
}
};
@sbussard : λ§μ΅λλ€.
λ΄ μ§λ¬Έμ defaultProps
μ΄ μ μ κ°μ²΄λ‘ ꡬνλλ κ²μ κ΄ν κ²μ
λλ€.
μλ§λ μμ±μμ μν΄ νΈμΆλλ ν¨μκ° λ μ μμ΅λλ€.
μ΅μ ν μ»΄νμΌλ¬κ° νΈμΆ μ¬μ΄νΈμμ μΈλΌμΈν μ μλλ‘ μλμ μΌλ‘ ν΄λμ€ μΈλΆμ μμ΅λλ€.
κ·Έλμ μ΅μ’ λ΅μ 무μμ λκΉ?
@gaearon μΈκΈ
μ΅μ ν μ»΄νμΌλ¬κ° νΈμΆ μ¬μ΄νΈμμ μΈλΌμΈν μ μλλ‘ μλμ μΌλ‘ ν΄λμ€ μΈλΆμ μμ΅λλ€.
νμ§λ§ "ν΄λμ€ μΈμ€ν΄μ€ νλ" λ°/λλ "ν΄λμ€ μ μ νλ"λ₯Ό μ¬μ©ν λ μ΅μ νν μ μλ€λ μλ―Έμ λκΉ? λλ μ΄λ€ μ κ·Ό λ°©μμ΄ μ νΈλκ³ μ κ·Έλ°μ§ μ΄ν΄νλ €κ³ λ Έλ ₯νκ³ μλ€κ³ μκ°ν©λλ€.
κ·Έλμ μ΅μ’ λ΅μ 무μμ λκΉ?
ES6μ κ³ μνκ³ μΆλ€λ©΄ 맨 μλμ ν λΉνμμμ€.
class MyComponent extends Component { /* ... */ }
MyComponent.defaultProps = { /* ... */ };
Create React Appμ μ¬μ© μ€μ΄κ±°λ μ€νμ ꡬ문μ μ΅μνκ³ ν΄λμ€ μμ± λ³νμ νμ±νν κ²½μ° λ€μμ μνν μ μμ΅λλ€.
class MyComponent extends Component {
static defaultProps = { /* ... */ };
/* ... */
}
μ΄λ¬ν μ κ·Ό λ°©μμ μμ ν λμΌν©λλ€. λ°μ½λ μ΄ν°λ μ¬μ©νλ κ²½μ° μ΄λ¬ν λ³νμ κ²°ν©νλ μ΄μν λ²κ·Έ κ° μμ μ μμ΅λλ€. λ°λΌμ ν΄λμ€ μμ±κ³Ό ν¨κ» λ°μ½λ μ΄ν°λ₯Ό μ¬μ©νλ κ²μ κΆμ₯νμ§ μμ΅λλ€.
μ΄κ² λμμ΄ λκΈΈ λ°λλ€!
νμ§λ§ "ν΄λμ€ μΈμ€ν΄μ€ νλ" λ°/λλ "ν΄λμ€ μ μ νλ"λ₯Ό μ¬μ©ν λ μ΅μ νν μ μλ€λ μλ―Έμ λκΉ? λλ μ΄λ€ μ κ·Ό λ°©μμ΄ μ νΈλκ³ μ κ·Έλ°μ§ μ΄ν΄νλ €κ³ λ Έλ ₯νκ³ μλ€κ³ μκ°ν©λλ€.
λ§μ§λ§μ ν λΉνλ κ²κ³Ό ν΄λμ€ μμ±μ μ¬μ©νλ κ²μλ μ°¨μ΄κ° μμ΅λλ€. ν λΉν ν΄λμ€ μμ± desugar. μ΄λ€ μ½λκ° μ»΄νμΌλλμ§ νμΈνλ €λ©΄ Babel μΉμ¬μ΄νΈμμ REPLμ μ¬μ©νμμμ€.
@romulofκ° μ΄μΌκΈ°νλ λμΌν λ¬Έμ μ λν΄ κΆκΈν©λλ€. λ¬Όλ‘ static defaultProps
λ₯Ό μ¬μ©νμ¬ κΈ°λ³Έ μμ±μ μ μν μ μμ§λ§ 컨ν
μ€νΈλ₯Ό μ¬μ©νλ €λ©΄ μ΄λ»κ² ν΄μΌ ν©λκΉ?
title
λ° onSubmit
μνμ΄ μλ μΌλ° λͺ¨λ¬ κ΅¬μ± μμκ° μμ΅λλ€. λλ‘λ κ°μ μνμΌλ‘ λ΄ μ±μ μμ ν λ€λ₯Έ λΆλΆμμ λͺ¨λ¬μ μ¬μ©νκ³ μΆμ΅λλ€. title
μ΄κ²μ μ μλν©λλ€.
class CreateUserModal extends Modal {
static defaultProps = { title: 'Create a new user' }
}
κ·Έλ¬λ μ μ ν μ½λ λ©μ΄λ¦¬μΈ onSubmit
νΈλ€λ¬λ μ°κ²° reduxμμ μ±μμ§ propμ μ¬μ©νκΈ° λλ¬Έμ 컨ν
μ€νΈκ° νμν©λλ€. 컨ν
μ€νΈλ₯Ό λΆλͺ¨ ν΄λμ€μ propsλ₯Ό μ€μ νλ λ° μ¬μ©ν μ μκΈ° λλ¬Έμ νμ¬ μ΄ μ½λλ₯Ό μΆμνν λ°©λ²μ΄ μμ΅λλ€.
λ΄κ°νλ €κ³ νλ κ²μ μ€μ λ‘ μν° ν¨ν΄μ΄ μλλ―λ‘ (λλ μ μ΄λ κ·Έλ μ§ μλ€κ³ μκ°ν©λλ€) λ€λ₯Έ λ°©λ²μ΄ μλμ§ κΆκΈν©λλ€.
λλ λ΄κ° κ°μ κ²μ ν μ μκΈ°λ₯Ό λ°λλ€.
class CreateUserModal extends Modal {
constructor (props) {
super({
title: 'Create a new user',
onSubmit: () => {
// do a load of stuff using props
}
})
}
}
νμ§λ§ μλνμ§ μμ΅λλ€.
@roberttod λ¬Έμ μ λν ν΄κ²°μ± μ μ°Ύμμ΅λκΉ? κ°μ¬ ν΄μ
νμ μ§λ¬Έμ νκ³ μΆμ΅λλ€.
λ°μ μ± λ§λ€κΈ°(react-scripts v1.0.12)λ₯Ό μ¬μ©νλ κ²½μ° μ΄κΈ° μνλ₯Ό λ€μκ³Ό κ°μ΄ μ μν΄λ λ©λκΉ?
class Foo extends Component {
static defaultProps = {};
static propTypes = {};
state = {...};
}
μμ±μλ₯Ό μ¬μ©νμ¬ μ΄κΈ° μνλ₯Ό λ€μκ³Ό κ°μ΄ μ μνλ κ²κ³Ό λΉκ΅νμ¬ μ°¨μ΄μ μ 무μμ λκΉ?
class Foo extends Component {
static defaultProps = {};
static propTypes = {};
constructor(props) {
super(props);
this.state = {...};
}
}
νΉν 첫 λ²μ§Έ μ κ·Ό λ°©μμ μ±λ₯μ μν₯μ΄ μμ΅λκΉ?
λ³λ° λ€λ₯Όκ² μμ΄ λκ°μ΅λλ€.
@jhabdas κΈμ, defaultPropsκ° λ€μκ³Ό κ°μ΄ μ μλ λ:
static defaultProps = {
}
μ΄ν ν΄λμ€ λ©μλμμ μ‘μΈμ€νλ λ°©λ²:
this.defaultProps
undefined
λ°νν©λλ€.
@majioa ν΄λΉ ν΄λμ€ μμ€ APIμ λν΄μλ λͺ¨λ₯΄μ§λ§ ν κ°μ§ μμ΄λμ΄λ κΈ°λ³Έ propsλ₯Ό λ³λμ κ°μ²΄λ‘ μΊμνκ³ μ°Έμ‘°νλ κ²μ λλ€. μ΄λ»κ² μκ°νλμ?
@majioa
class Foo extends React.Component {
static defaultProps = { param: 1 };
render() {
return Foo.defaultProps.param;
}
}
κ°μ λ¬Έμ κ° λ°μνλ©΄ babel-plugin-transform-class-propertiesλ₯Ό μ€μΉν΄μΌ
npm install -D babel-plugin-transform-class-properties
κ·Έλ° λ€μ .babelrcμ "transform-class-properties"
λ₯Ό μΆκ°ν©λλ€.
{
"presets": ["env", "react"],
"plugins": ["react-hot-loader/babel", "transform-class-properties"]
}
μ μ get defaultProps() {
λ°ν {
μλμ°¨: "λ©λ₯΄μΈλ°μ€"
}
@Arm7107 μμκ° μμ±λ λλ§λ€ μλ‘μ΄ κ°μ²΄λ₯Ό μμ±νκΈ° λλ¬Έμ λΉν¨μ¨μ μ λλ€. λλ μ΄κ²μ μ¬μ© νμ§ μλ κ²μ΄ μ’μ΅λλ€. μμ±μ ν λΉν©λλ€.
MyComponent.defaultProps = {
// ...
}
λλ μ€νμ μ μ ν΄λμ€ μμ± κ΅¬λ¬Έμ μ¬μ©νμμμ€.
class MyComponent extends React.Component {
static defaultProps = {
// ...
};
// ...
}
(λ°λ²¨ νλ¬κ·ΈμΈμ΄ νμν©λλ€)
Google κ²μκ²°κ³Όμ μλͺ»λ μ μμ΄ λ μ΄μ νμλμ§ μλλ‘ μ΄ λ¬Έμ λ₯Ό μ κ·Έκ² μ΅λλ€.
κ°μ₯ μ μ©ν λκΈ
λ§μ΅λλ€. λμ λ€μκ³Ό κ°μ΄ μμ±ν©λλ€.
κ·Έκ² λΉμ μ μν΄ μλν©λκΉ?