2017-08-17 51 views
4

我在渲染中使用了開關複選框,因此檢測它是否爲checked的一個重要屬性是defaultChecked。我之前在componentWillReceiveProps中設置了狀態。我嘗試先將狀態作爲屬性,但在編譯代碼爲babel.js時出現錯誤Unexpected token。在我嘗試使用dangerouslySetInnerHTML之後,它仍然沒有工作(底部錯誤)。如何在ReactJs中將狀態用作檢查的屬性?

首先嚐試:

<input type="checkbox" name="onoffswitch" {this.state.required} /> 

App.jsx

class App extends React.Component { 
    constructor(props) { 
     super(props); 
     this.state = { 
      required: '' 
     }; 
    }; 

    componentWillReceiveProps(nextProps){ 
     //more code  
     this.setState({ 
      required: 'defaultChecked' 
     }); 
    }; 

    render() { 
     return(
     <div id="section">     
      <div className="bottom-question"> 
       <div> 
        <div className="onoffswitch-add pull-left"> 
         <input type="checkbox" 
           name="onoffswitch" 
           dangerouslySetInnerHTML={this.state.required} /> 

         <label className="onoffswitch-label-add" htmlFor="switch-required"> 
          <div className="onoffswitch-inner-add"></div> 
          <div className="onoffswitch-switch-add"></div> 
         </label> 
        </div> 
       </div> 
      </div> 
     </div> 
    ) 
    } 
} 

錯誤:

The full text of the error you just encountered is: 

input is a void element tag and must neither have `children` 
nor use `dangerouslySetInnerHTML`. Check the render method of EditInput 
+0

檢查這個曾經有關如何使用複選框以reactjs https://facebook.github.io/react/docs/forms.html –

回答

2

什麼你想要它(在這種情況下,你使用字符串'defaultChecked'),你需要告訴輸入檢查自己或沒有根據。

我會建議你重構狀態變量是一個布爾值,並調用它checked像這樣

class App extends React.Component { 
    constructor(props) { 
     super(props); 
     this.state = { 
      checked: false 
     }; 
    }; 

    componentWillReceiveProps(nextProps){ 
     //more code  
     this.setState({ 
      checked: true 
     }); 
    }; 

    render() { 
     return(
     <div id="section">     
      <div className="bottom-question"> 
       <div> 
        <div className="onoffswitch-add pull-left"> 
         <input type="checkbox" 
           name="onoffswitch" 
           checked={this.state.checked} /> 

         <label className="onoffswitch-label-add" htmlFor="switch-required"> 
          <div className="onoffswitch-inner-add"></div> 
          <div className="onoffswitch-switch-add"></div> 
         </label> 
        </div> 
       </div> 
      </div> 
     </div> 
    ) 
    } 
} 
+1

upvoted解密一個不好問的問題:) – Ted

+2

@Ted對不起。一個字會產生差異。謝謝約翰。 –

+0

@泰德謝謝:)有時候會在線條之間閱讀!儘管天使,但不用擔心......它發生在每個人:)感謝您的快速接受! –

1

我想你想<input required={this.state.required} />

+0

請大家作爲參考我的意見在@ XPLOT1ON答案。 –

+0

在這種情況下,你可以使用'disabled'而不是'required'來做同樣的事情。 – ChemicalRocketeer

1

陣營有此屬性。

<input type="checkbox" name="onoffswitch" disabled={this.state.required} /> 

其中this.state.required是您要使用checked屬性來檢查適用於JavaScript的

<input type="checkbox" name="onoffswitch" checked={this.state.required === 'defaultChecked'} /> 

當你設置成所需要的狀態的複選框,輸入一個boolean

+0

我認爲有一個與'required'屬性有關的混淆,因爲該單詞在代碼中。但是我需要的是根據那個狀態顯示'切換按鈕'是否激活。 –

+0

這不是反應的特殊屬性,它只是一個標準的html屬性 – ChemicalRocketeer

+0

@AngelCuenca我剛剛更新了我的答案,禁用而不是必需的。 – XPLOT1ON

0

你會希望有一個disabled道具爲您的按鈕就是true時未選中該複選框,當它是和false

爲此,請從存儲複選框值的狀態變量開始。然後添加一個在onChange事件中觸發的函數,該函數將狀態從true-> false切換,反之亦然。

當該複選框未選中時,該按鈕應該被禁用。

class MyApp extends React.Component { 
 
    constructor() { 
 
    super(); 
 
    this.state = { 
 
     checkboxState: false 
 
    }; 
 
    } 
 
    
 
    toggleCheckbox =() => { 
 
    this.setState({checkboxState: !this.state.checkboxState}); 
 
    } 
 
    
 
    render() { 
 
    return (
 
     <form> 
 
     <input type="checkbox" name="onoffswitch" onChange={this.toggleCheckbox} /> 
 
     <input type="submit" disabled={!this.state.checkboxState} value="submit" /> 
 
     </form> 
 
    ); 
 
    } 
 
} 
 

 
ReactDOM.render(<MyApp />, 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

請在@ XPLOT1ON答案中參考我的評論。 –

+0

@AngelCuenca,這沒有任何幫助嗎? – Chris

+0

@Cris我不這麼認爲。我只需要控制它是否被「檢查」或不是按鈕。不禁用或不。 –