javascript - 如何在 React 中执行函数?
问题描述
我是 React 的初学者,请有人帮忙,我写了这个函数:
import React, {
Component }
from 'react';
import './Grid.css';
import test from "../product.json";
import $ from 'jquery';
class Grid extends Component {
makeGrid = () =>{
var data = test.map( x => [ x.name, x.price, ] );
$( document ).ready(function() {
console.log( "ready!" );
$('.boxes').append('<div className = "boxi"> Success </div>')
});
我想执行这个功能,所以我尝试了:
render() {
return (
<div className = "Grid" >
< section className = "boxes" >
< /section> < /div > );
<h2> {this.makeGrid}</h2>
}
}
export default Grid;
但即使console.log 不起作用也没有任何反应。请有人帮助我。
解决方案
给你一个解决方案
import React, { Component } from 'react';
import './Grid.css';
import test from "../product.json";
import $ from 'jquery';
class Grid extends Component {
constructor(props) {
this.makeGrid = this.makeGrid.bind(this);
}
makeGrid = () => {
var data = test.map( x => [ x.name, x.price, ] );
console.log( "ready!" );
$('.boxes').append('<div className = "boxi"> Success </div>')
}
render() {
return (
<div className="Grid">
<section className="boxes">
</section>
< /div >
<h2>
{this.makeGrid()}
</h2>
);
}
}
export default Grid;
您需要绑定this
到 makeGrid 方法。
推荐阅读
- javascript - 在Javascript中查找要更改的位数以获得等效的二进制数
- roblox - 房子怎么定位?罗布乐思
- protractor - 如何在分片测试时仅使用量角器和黄瓜生成一份报告?
- sql - 从该表中选择正确数据的有效方法是什么?
- javascript - 如何使用 SWR 对具有嵌套数组的复杂对象进行变异?
- c# - 没有提升权限的 Windows 文件关联
- mysql - 视窗/MySQL 5.5。/ 在异常 0xc0000005 上启动时崩溃
- postgresql - 在 DBeaver 或 PgAdmin 中看不到引发消息
- java - 如何消除spring boot中bean的重复性能?
- node.js - 从猫鼬对象数组中的最后一个元素的(对象)字段中查找