简体   繁体   English

在React模板渲染上调用函数

[英]Call function on React template render

I need the template questionsCollected to call the getQuestions function when the template is rendered. 我需要模板questionsCollected来在呈现模板时调用getQuestions函数。 I am able to call a function when an event is fired, but in this case I want the function to fire when the template is rendered and populate the options in the select menu. 我可以在触发事件时调用函数,但是在这种情况下,我希望函数在呈现模板并填充选择菜单中的选项时触发。

  • The ajax call is successful and returns the option items. ajax调用成功,并返回选项项。 I can log the return to the console. 我可以将返回值记录到控制台。
  • The shell of the template is successfully rendering to the page. 模板的外壳已成功呈现到页面。

How do I call a function from within the template without utilizing an event (onClick, etc)? 如何在不利用事件(onClick等)的情况下从模板内调用函数?

Thanks! 谢谢!

                 class myClass extends React.Component {
                      constructor () {
                        super()

                        this.state = {
                          isActive: false,
                        }
                        this.getQuestions = this.getQuestions .bind(this)
                      }


                  getQuestions () {
                    const token = `xxxx`
                    const url = 'https://api.com' + token

                    Ajax.get(url).then(function (response) {
                      const data = JSON.parse(response.response)

                      const questions = []

                      Object.keys(data).forEach(function (value, key) {
                        questions.push(<option> + data.features[key].properties.question + </option>)
                      })

                      return questions
                    })
                  }

              render () {        
                return <menuItems
                  children={this.renderChildren()}
                />
              }
              renderChildren () {
               const questionsCollected = (
                  <div key='questionText' id='question'>
                    <select>
                      <option>Questions</option>
                      {this.getQuestions}
                    </select>
                  </div>
                )
               return [questionsCollected]
            }


    export default myContainer

Are you familiar with the react lifecycle? 您熟悉反应生命周期吗? Check out this page, I refer to it often: 签出此页面,我经常提到它:

http://busypeoples.github.io/post/react-component-lifecycle/ http://busypeoples.github.io/post/react-component-lifecycle/

I think you want to move the ajax call to componentDidMount. 我认为您想将ajax调用移至componentDidMount。 Upon its success you can call setState to set your questions from the ajax call in state, then your render method reads from the state object for the questions. 成功后,您可以调用setState来从状态的ajax调用中设置问题,然后您的render方法从状态对象读取问题。 Kind of like this: 有点像这样:

https://daveceddia.com/ajax-requests-in-react/ https://daveceddia.com/ajax-requests-in-react/

Hope that helps 希望能有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM