简体   繁体   English

将 Fetch 与 React 组件一起使用

[英]Using Fetch with React Compnent

I'm trying to use fetch to retrieve my data, and then setState to apply it in the componentDidMount.我正在尝试使用 fetch 来检索我的数据,然后 setState 将其应用到 componentDidMount 中。 I'm getting the log that says the component mounted, anything after the Fetch, included the simple console logs are not working.我收到的日志显示组件已安装,Fetch 之后的任何内容,包括简单的控制台日志都不起作用。 Is there something simple wrong with my syntax?我的语法有什么简单的问题吗? I have verified that the api does work.我已经验证 api 确实有效。

import React, { Component } from 'react';
import { Editor } from '@tinymce/tinymce-react';


let policyContent = '';

class TinyEditor extends Component {
    constructor(props) {
        super(props);

this.state = { content: '' };

    this.handleEditorChange = this.handleEditorChange.bind(this);
    this.handleClick = this.handleClick.bind(this);

}

componentDidMount() {
    console.log(`Component did mount`);
    fetch("/policy")
        .then(res => res.json())
        .then((result) => {
            console.log(result);
            console.log(`You got your results... now what?`);
            policyContent = result;
            console.log(policyContent[0]);
            this.setState({policyContent});
        });
}

handleEditorChange(content, editor) {
    this.setState({ content });
}

handleClick(e) {
    e.preventDefault();
    console.log(`Button was clicked.`);
    console.log(this.state.content);
}

render() {
    return (
        <div>
            <div className="container">
                <Editor
                    apiKey='yf9eajz93s3akrlb24b8ja9xcszddbxx22x4ug8c2q5boxw3'
                    className="mceEditor"
                    id='myTextArea'
                    init={{
                        height: 500,
                        editor_selector: 'mceEditor',
                        menubar: false,
                        browser_spellcheck: true,
                        contextmenu: true,
                        branding: false,
                        plugins: [
                            'advlist autolink lists link image charmap print preview anchor',
                            'searchreplace visualblocks code fullscreen',
                            'insertdatetime media table paste code help wordcount'
                        ],
                        toolbar:
                            'undo redo | formatselect | bold italic backcolor | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | help'
                    }}
                    onEditorChange={this.handleEditorChange}
                    value={this.state.content}
                />
                <button onClick={this.handleClick}>Save</button>
            </div>
        </div>
    )
}

} }

export default TinyEditor;导出默认的 TinyEditor;

I have run your code using a online api.我已经使用在线 api 运行了您的代码。 every thing is working fine.一切正常。 Please replace the code of componentDidMount and check the output for testing.请更换componentDidMount的代码并检查output进行测试。

componentDidMount() {
        console.log(`Component did mount`);
        fetch(`https://opentdb.com/api.php?amount=10&difficulty=hard&type=boolean`)
            .then(res => res.json())
            .then((result) => {
                console.log(result);
                console.log(`You got your results... now what?`);
                policyContent = result.results[0];
                console.log(policyContent);
                this.setState({content: policyContent.category});
            });
    }

Please check the output screen where the text of Editor is coming from api.请检查 output 屏幕,其中编辑器的文本来自 api。

在此处输入图像描述

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

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