1
如何設置「檢查」,當我有props.a?我不能老是知道如何寫的東西,像這樣:如何在輸入中使用?
<input if (props.a > x) {checked='checked'}/>
我試過<input {props.a > x ? 'checked' : ''}/>
,但我有一個錯誤
如何設置「檢查」,當我有props.a?我不能老是知道如何寫的東西,像這樣:如何在輸入中使用?
<input if (props.a > x) {checked='checked'}/>
我試過<input {props.a > x ? 'checked' : ''}/>
,但我有一個錯誤
可以asssigned檢查狀態值,每當道具改變 像
componentWillReceiveProps(nextProps) {
if(nextProps.a > x) {
this.setState({checked: 'checked'});
}
}
<input checked={this.state.checked}/>
在componentWillReceiveProps
功能控制這種狀態,因爲它會被調用,否則你可以直接在放道具像
<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>
你應該使用<input checked={ (props.a > x) } />
。
你試過這樣嗎? ' x? 'checked':null />'或者可能是' x? true:false />' –