2017-03-07 60 views
0

在我的父組件中我有一個表單,我需要將該表單的輸入傳遞給子組件,我需要將它附加到API。我一直在試圖做這樣:將子組件中的道具添加到API

`https://api.icndb.com/jokes/random?=firstName${this.props.name}`

,但我不斷收到此錯誤:jQuery.Deferred例外:this.state.jokes.map不是一個函數類型錯誤:此.state.jokes.map不是函數

當我不向API添加道具時,map函數可以工作。

全部代碼在這裏:

家長:

class Input extends Component { 
 
    constructor(props){ 
 
     super(props) 
 
     this.state = { 
 
     value: ' ', 
 
     selected: false 
 
     } 
 
     this.handleSubmit = this.handleSubmit.bind(this); 
 
     this.handleChange = this.handleChange.bind(this); 
 
    } 
 

 
    handleChange(event) { 
 
    this.setState(
 
    {value: event.target.value} 
 
    ); 
 
    } 
 

 

 

 
    handleSubmit(event) { 
 
    this.setState(
 
     {selected: true} 
 
    ) 
 
    } 
 

 

 
    render() { 
 
    return (
 
     <div> 
 
     <img src="http://vignette3.wikia.nocookie.net/mugen/images/b/b6/Chuck-norris.png/revision/latest?cb=20120615045306" alt="chuck"/> 
 
      <form> 
 
      <h1>Whats your name?</h1> 
 
       <input type="text" value={this.state.value} onChange={this.handleChange}/> 
 
      </form> 
 
      <Button onClick={this.handleSubmit} id="submit"> 
 
       <Link to="/jokes">Submit</Link> 
 
      </Button> 
 
      {this.state.selected === true 
 
       ? 
 
      <Jokes name={this.state.value} /> 
 
       : 
 
       null 
 
       } 
 
     </div> 
 
    ) 
 
    } 
 

 
    }

孩子:

class Jokes extends Component { 
 
    constructor(props){ 
 
    super(props); 
 
    this.state = {jokes: []} 
 
    this.JokeList = this.JokeList.bind(this) 
 
    } 
 

 
    componentDidMount() { 
 
    this.JokeList() 
 
    } 
 

 
    JokeList() { 
 
    return $.getJSON(`https://api.icndb.com/jokes`) 
 
     .then((data) =>{ 
 
     this.setState({ jokes: data.value }) 
 
     console.log(this.state.jokes) 
 
     }); 
 
    } 
 

 
    render(){ 
 
     const funnies = this.state.jokes.map((item, i) => { 
 
     return <div id="quoteList"> 
 
       <h1 id="num" className="answers" key="id">{item.id}</h1> 
 
       <h2 id="quote" className="answers" key="yoke">{item.joke}</h2> 
 
       </div> 
 
     }) 
 
    return(
 

 
     <div> { funnies } </div> 
 

 
    ) 
 
    } 
 
}

任何幫助非常感謝!

+0

我不知道你想在這裏做什麼?你可以通過從母公司任何道具儘管如此,你可以在任何地方訪問它們。 – paqash

+0

@paqash我把它改寫成對我的問題更具體 – melr

+1

你可以在調用'this.setState({jokes:data.value})之前檢查'data.value'' – paqash

回答

0

我檢查了發生了什麼事。

當您請求https://api.icndb.com/jokes/random?=firstName${this.props.name}時,由於過濾條件的限制,很可能只返回一條記錄。

在這種情況下,API將返回一個Object,而不是一個元素爲的Array。這就是你錯誤的原因:

this.state.jokes.map is not a function

不過,我調整好你的代碼來支持這兩種情況下,請檢查下面的例子。下面是我做的,一旦我們得到的迴應:

// Make sure the jokes are an Array, because of we iterate over them later. 
let jokes = Array.isArray(data.value) ? data.value : [data.value]; 
this.setState({ jokes }); 

class Input extends React.Component { 
 
    constructor(props){ 
 
     super(props) 
 
     this.state = { 
 
     value: ' ', 
 
     selected: false 
 
     } 
 
     this.handleSubmit = this.handleSubmit.bind(this); 
 
     this.handleChange = this.handleChange.bind(this); 
 
    } 
 

 
    handleChange(event) { 
 
    this.setState(
 
    {value: event.target.value} 
 
    ); 
 
    } 
 

 

 

 
    handleSubmit(event) { 
 
    this.setState(
 
     {selected: true} 
 
    ) 
 
    } 
 

 

 
    render() { 
 
    return (
 
     <div> 
 
     <img src="http://vignette3.wikia.nocookie.net/mugen/images/b/b6/Chuck-norris.png/revision/latest?cb=20120615045306" alt="chuck"/> 
 
      <form> 
 
      <h1>Whats your name?</h1> 
 
       <input type="text" value={this.state.value} onChange={this.handleChange}/> 
 
      </form> 
 
      <input type="button" value="Submit" onClick={this.handleSubmit} id="submit" /> 
 
      {this.state.selected === true 
 
       ? 
 
      <Jokes name={this.state.value} /> 
 
       : 
 
       null 
 
       } 
 
     </div> 
 
    ) 
 
    } 
 

 
    } 
 
    
 
class Jokes extends React.Component { 
 
    constructor(props){ 
 
    super(props); 
 
    this.state = {jokes: []} 
 
    this.JokeList = this.JokeList.bind(this) 
 
    } 
 

 
    componentDidMount() { 
 
    this.JokeList() 
 
    } 
 

 
    JokeList() { 
 
    return $.getJSON(`https://api.icndb.com/jokes/random?=firstName${this.props.name}`) 
 
     .then((data) =>{ 
 
     // Make sure the jokes are an Array, because of we iterate over them later. 
 
     let jokes = Array.isArray(data.value) ? data.value : [data.value]; 
 
     this.setState({ jokes }); 
 
     }); 
 
    } 
 

 
    render(){ 
 
     const funnies = this.state.jokes.map((item, i) => { 
 
     return <div id="quoteList"> 
 
       <h1 id="num" className="answers" key="id">{item.id}</h1> 
 
       <h2 id="quote" className="answers" key="yoke">{item.joke}</h2> 
 
       </div> 
 
     }) 
 
    return(
 

 
     <div> { funnies } </div> 
 

 
    ) 
 
    } 
 
} 
 

 
ReactDOM.render(<Input />, document.getElementById('container'));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<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="container"></div>