相关疑难解决方法(0)

在地图函数Reactjs中,"this"未定义

我正在使用Reactjs编写一个菜单组件.

"use strict";

var React = require("react");
var Menus = React.createClass({

    item_url: function (item,categories,articles) {
        console.log('afdasfasfasdfasdf');
        var url='XXX';
        if (item.type == 1) {
            url = item.categoryId == null ? 'javascript:void(0)' : path('buex_portal_browse_category', {slug: categories[item.categoryId].slug});
        } else if (item.type == 2) {
            url = item.articleId == null ? 'javascript:void(0)' : path('buex_portal_view_article', {slug: articles[item.articleId].slug, id: item.articleId});
        } else {
            url = item.url;
        }
        return url;
    },

    render: function () {
     //   console.log(this.props.menus);  // return correctly
            var menuElements = this.props.menus.map(function (item1) { // …
Run Code Online (Sandbox Code Playgroud)

javascript this reactjs map-function

94
推荐指数
1
解决办法
4万
查看次数

React函数说“不是函数”

我正在努力将一些React应用分解为较小的组件。在分离代码之前,一切都按计划进行。我现在正在尝试调用一个函数onChange,该函数先调用一个函数,然后再将其作为调用prop。我正在像这样绑定函数,this.updateInput = this.updateInput.bind(this);但是我仍然无法弄清缺少的内容。我在这里尝试了最近的文章(React:将功能传递给子组件),但错误仍然存​​在。任何帮助都很棒。

这是我正在使用的代码:

class Weather extends React.Component {
  constructor(props) {
    super(props);

    this.state = {
      city: '',
      details: []
    };

    this.updateInputValue = this.updateInputValue.bind(this);
  }

  updateInputValue(e) {
    this.setState({
      city: e.target.value
    });
    console.log('hit')
  }

  render() {
    return (
      <div className={style.container + ' ' + style.bodyText}>
        <WeatherForm
          updateInput={this.updateInputValue}
        />
      </div>
    );
  }
}


class WeatherForm extends React.Component {
  constructor(props) {
    super(props);
    this.updateInput = this.updateInput.bind(this); 
  }

  updateInput(e) {
    this.props.updateInputValue(e);
  }

  render() {
    return (
      <div className={style.weatherForm}> …
Run Code Online (Sandbox Code Playgroud)

javascript reactjs

6
推荐指数
2
解决办法
6250
查看次数

标签 统计

javascript ×2

reactjs ×2

map-function ×1

this ×1