2017-03-01 3 views
1

props.a를 가지고있을 때 어떻게 '체크'할 수 있습니까? 입력시 사용 방법은 무엇입니까?

<input if (props.a > x) {checked='checked'}/> 

내가 <input {props.a > x ? 'checked' : ''}/>을 시도하지만 오류를

+0

당신은 이것을 좋아합니까? ' x? 'checked': null />'또는' x? true : false />' –

답변

0

당신은 상태 값을 확인하고가 호출 될 것이다 소품

처럼
componentWillReceiveProps(nextProps) { 
    if(nextProps.a > x) { 
     this.setState({checked: 'checked'}); 
    } 

} 

<input checked={this.state.checked}/> 

을 변경할 때마다 componentWillReceiveProps 기능이 상태를 제어하거나 다른 당신이

같은 소품에 넣어 직접 값을 반환 할 수 있습니다 asssigned 수 있습니다
<input checked={(props.a > x)? 'checked': null}}/> 

class App extends React.Component { 
 
    render() { 
 
    var a = 10; 
 
    var x = 9; 
 
    return (
 
     <div> 
 
     <input type="checkbox" checked={(a > x)? 'checked': null}/></div> 
 
    ) 
 
    } 
 
} 
 

 
ReactDOM.render(<App/>, 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"></div>

,
0

을 당신은 <input checked={ (props.a > x) } />를 사용해야했다 :이 같은 뭔가를 작성하는 방법을 이해은`t.