从另一个类调用静态方法onclick react

时间:2017-03-20 09:02:30

标签: reactjs static-methods

我想从另一个类onclick调用静态方法。我有三个组成部分。第一个是,

#include <iostream>
#include <wchar.h>

#include "unicode/ustring.h"

void convertUnicodeStringtoWideChar(const UChar* cuniszSource,
                                    const int32_t cunii32SourceLength,
                                    wchar_t*& rpwcharDestination,
                                    int32_t& destCapacity)
{
  UErrorCode uniUErrorCode = U_ZERO_ERROR;

  int32_t pDestLength = 0;

  rpwcharDestination     = 0;
  destCapacity = 0;

  u_strToWCS(rpwcharDestination,
             destCapacity,
             &pDestLength,
             cuniszSource,
             cunii32SourceLength,
             &uniUErrorCode);

  uniUErrorCode = U_ZERO_ERROR;
  rpwcharDestination = new wchar_t[pDestLength+1];
  if(rpwcharDestination)
  {
    destCapacity = pDestLength+1;

    u_strToWCS(rpwcharDestination,
               destCapacity,
               &pDestLength,
               cuniszSource,
               cunii32SourceLength,
               &uniUErrorCode);

    destCapacity = wcslen(rpwcharDestination);
  }
} //function ends

int main()
{
    //                     a       ä       Š       €    (          )
    UChar input[20] = { 0x0061, 0x00e4, 0x0160, 0x20ac, 0xd87e, 0xdd29, 0x0000 };
    wchar_t * output;
    int32_t outlen = 0;
    convertUnicodeStringtoWideChar( input, 6, output, outlen );
    for ( int i = 0; i < outlen; ++i )
    {
        std::cout << std::hex << output[i] << "\n";
    }
    return 0;
}

然后我有另一个组件,我正在加载此组件

    import * as React from 'react';
    import { Link } from 'react-router';

    export var One= React.createClass<any, any>({
        getInitialState: function () {
            return (
                {
                    name: ""
                });
        },

        statics: {
            updateName:function(){
                this.setState({name:'jobs changed'});
            },
        },
        render() {
                   return(
                          <div >
                        <span>{this.state.name}</span>
                    </div>
                   );
       }
 });

});

我有第三个组件,点击一个应该调用的静态方法。

import * as React from 'react';
import {One} from './data';
export var Two= React.createClass<any, any>({
    render() {
               return(
                      <div >
                    <One />
                </div>
               );
   }

});

他们在这里给我以下错误:

import * as React from 'react';
import { Link } from 'react-router';
import {One} from './data';

export var Three= React.createClass<any, any>({
    render() {
               return(
                      <div >
                   <Link onClick={One.updateName} >Click Me </Link>
                </div>
               );
   }

请帮忙怎么做?我们可以在静态方法中使用Error:Property 'updateName' does not exist on type 'ClassicComponentClass<any>' 吗?如果没有那么我如何从静态方法更新状态?或者我是否需要在课外使用名称变量,然后使用它来更新名称? 我正在尝试基于此fiddle

执行此操作

2 个答案:

答案 0 :(得分:2)

静态方法与实例方法之间的区别

  1. 实例方法是需要先创建其类的对象才能调用它的方法。静态方法是Java中无需创建类对象即可调用的方法。
  2. 静态方法使用static关键字声明。实例方法不带有static关键字。
  3. 静态方法表示将作为类的单个副本存在。但是实例方法存在多个副本,具体取决于为该类创建的实例数量。
  4. 可以使用类引用来调用
  5. 静态方法。实例或非静态方法是通过使用对象引用来调用的。
  6. 静态方法无法直接访问实例方法和实例变量。实例方法可以直接访问静态变量和静态方法。

答案 1 :(得分:0)

我意识到静态方法不能拥有自己类的引用。这个问题不是一个有效的问题。

相关问题