为什么此React方法无法按预期工作?

时间:2019-11-28 23:46:18

标签: javascript reactjs typescript react-native

我有一些代码旨在消除onFocus上的占位符,并在onBlur上返回它们,对于登录文本输入似乎正常,但对于密码1则不能。您介意看一下代码吗?

这是有问题的方法

#include<iostream>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <GL/gl.h>     // openGL header
#include <GL/glu.h>   // glut header
#include <GL/glut.h>   // glut header
#include <string>
#include <time.h>
//#include <Math.h>
#define STB_IMAGE_IMPLEMENTATION



/////////////////////////////////////Textures==============================================/////////////////////////////////////
#include "stb_image.h"
//GLuint texture; //the array for our texture

// angle of rotation for the camera direction
float angle=0.0;
// actual vector representing the camera's direction
float lx=0.0f,lz=-1.0f;
// XZ position of the camera
float x=1.0f,z=3.0f;


GLdouble ox = 0.0, oy = 0.0, oz = 0.0;
int **array;


/*void Mouse_pos(int button, int state, int x, int y) {

    GLint viewport[4];

    GLdouble modelview[16], projection[16];

    GLfloat wx = x, wy, wz;

    if (state != GLUT_UP)

        return;

    if (button == GLUT_RIGHT_BUTTON)

        exit(0);

    glGetIntegerv(GL_VIEWPORT, viewport);

    y = viewport[3] - y;

    wy = y;

    glGetDoublev(GL_MODELVIEW_MATRIX, modelview);

    glGetDoublev(GL_PROJECTION_MATRIX, projection);

    glReadPixels(x, y, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &wz);

    gluUnProject(wx, wy, wz, modelview, projection, viewport, &ox,
        &oy, &oz);



    glutPostRedisplay();





}




void mouse_handler(int button, int state, int x, int y) {
    if ((button == GLUT_LEFT_BUTTON ) && (state == GLUT_UP)) { // Pause/resume


            glutMouseFunc(Mouse_pos);



    }

}*/

void loadTextureFromFile(const char *filename, unsigned int texture)
{
    glBindTexture(GL_TEXTURE_2D, texture);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    // load and generate the texture
    int width, height, nrChannels;
    unsigned char *data = stbi_load(filename, &width, &height, &nrChannels, 0);
    if (data)
    {
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0,
                     GL_RGB, GL_UNSIGNED_BYTE, data);
        //glGenerateMipmap(GL_TEXTURE_2D);
    }
    else
    {
        std::cout << "Failed to load texture" << std::endl;
    }
    stbi_image_free(data);
}

//for loading the texture
const char* filename1 = "rock.bmp";
const char* filename2 = "paper.bmp";
const char* filename3 = "scissors.bmp";
unsigned int tob[3];

/*void FreeTexture(GLuint tob)
{
    glDeleteTextures(3, &tob);
}*/



/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


static void
drawBox(GLfloat size, GLenum type)
{
    static GLfloat n[6][3] =
    {
        {-1.0, 0.0, 0.0},
        {0.0, 1.0, 0.0},
        {1.0, 0.0, 0.0},
        {0.0, -1.0, 0.0},
        {0.0, 0.0, 1.0},
        {0.0, 0.0, -1.0}
        };

    static GLint faces[6][4] =
    {
        {0, 1, 2, 3},
            {3, 2, 6, 7},
            {7, 6, 5, 4},
        {4, 5, 1, 0},
        {5, 6, 2, 1},
        {7, 4, 0, 3}
    };
    GLfloat v[8][3];
    GLint i;

    v[0][0] = v[1][0] = v[2][0] = v[3][0] = -size / 2;
    v[4][0] = v[5][0] = v[6][0] = v[7][0] = size / 2;
    v[0][1] = v[1][1] = v[4][1] = v[5][1] = -size / 2;
    v[2][1] = v[3][1] = v[6][1] = v[7][1] = size / 2;
    v[0][2] = v[3][2] = v[4][2] = v[7][2] = -size / 2;
    v[1][2] = v[2][2] = v[5][2] = v[6][2] = size / 2;

    for (i = 5; i >= 0; i--) {
        glBegin(type);
            glNormal3fv(&n[i][0]);

        glTexCoord2d(0.0,0.0); //for texturing
            glVertex3fv(&v[faces[i][0]][0]);
        glTexCoord2d(0.0,1.0);
            glVertex3fv(&v[faces[i][1]][0]);
        glTexCoord2d(1.0,1.0);
            glVertex3fv(&v[faces[i][2]][0]);
        glTexCoord2d(1.0,0.0);
            glVertex3fv(&v[faces[i][3]][0]);
            glEnd();
    }
}

void APIENTRY
myglutSolidCube(GLdouble size)
{
    //glBindTexture(GL_TEXTURE_2D, tob[1]); 
    drawBox(size, GL_QUADS);
}

//int red_color[]={255,0,0};
//int blue_colot[]={0,0,255};


//////////////////////////////=========MENU============/////////////
enum MENU_TYPE //menu options-values
{
        MENU_START,
        MENU_EXIT,

};

//create the menu - Prototype
void my_createmenu(void);
// Menu handling function declaration - Prototype
void menu(int);



void init()
{

    //for 3d lighting
    glEnable(GL_DEPTH_TEST); //depth test
    glEnable(GL_LIGHTING); //enable light from a single source
    glEnable(GL_LIGHT0); //enable white light , diffuse and specular components
    glEnable(GL_COLOR_MATERIAL); //track the current color
    //glEnable(GL_TEXTURE_2D);  
}


void display()
{




    glClearColor(0.0f, 0.0f, 0.0f, 1.0f); //Black and opaque
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    //define the projection matrix just once and use the modelview matrix all other times


    glMatrixMode(GL_PROJECTION);  //Applies subsequent matrix operations to the projection matrix stack
    glLoadIdentity();//Reset

    GLint viewport[4];
    glGetIntegerv(GL_VIEWPORT, viewport); //The params parameter returns four values: the x and y window coordinates of the viewport, followed by its width and height
    double aspect = (double)viewport[2] / (double)viewport[3]; // y/width would be 1.0

    gluPerspective(60,aspect, 1, 100); //using perspective projection
    //gluOrtho2D(0.0,600.0,-60.0,600.0);  


    glMatrixMode(GL_MODELVIEW); //for trasformations - Applies subsequent matrix operations to the texture matrix stack
    glLoadIdentity();

    // move back a bit for viewer  , cause of gluPerspective
    glTranslatef( 0, 0, -35 );


    float e=0,f=0;

    //construct the grid with reference the central cube  
    for(int i=0;i<8;i++) {
        for(int j=0;j<8;j++) {  


            glPushMatrix();
                glTranslatef(0.0f+e,0.0f+f,0.0f); //right and below
                glRotatef(20.0f,1.0f,-2.0f,0.0f); //looking 3d
                    glColor3ub(245, 245, 220); //Beige
                    glutSolidCube(2.25);
                glPopMatrix();


            glPushMatrix();
                glTranslatef(0.0f-e,0.0f+f,0.0f); //left and below
                glRotatef(20.0f,1.0f,-2.0f,0.0f); //looking 3d
                    glColor3ub(245, 245, 220); //Beige
                    glutSolidCube(2.25);
                glPopMatrix();


            glPushMatrix();
                glTranslatef(0.0f+e,0.0f-f,0.0f); //right and up
                glRotatef(20.0f,1.0f,-2.0f,0.0f); //looking 3d
                    glColor3ub(245, 245, 220); //Beige
                    glutSolidCube(2.25);
                glPopMatrix();


            glPushMatrix();
                glTranslatef(0.0f-e,0.0f-f,0.0f); //left and up
                glRotatef(20.0f,1.0f,-2.0f,0.0f); //looking 3d
                    glColor3ub(245, 245, 220); //Beige
                    glutSolidCube(2.25);
                glPopMatrix();


                f += -2.63;
            }
            f=0;
            e+=2.63;
        }



    glutSwapBuffers(); //implicit   glFlush



}

//function for creating random cube between red,blue,rock,paper,scissors
int myRandom(void) {
    unsigned int rand_num,rand_num2,rand_num3;  

    rand_num=(rand()%5)+1;



    return rand_num;
}

void handle_myRandom(int rand_num) {



    if(rand_num==1) {
        glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
        glColor3ub(245, 245, 220); //beige
        glEnable(GL_TEXTURE_2D);   //enable texture
        glBindTexture(GL_TEXTURE_2D, tob[0]); //rock
    }
    else if(rand_num==2) {  
        glClearColor(0.0f, 0.0f, 0.0f, 1.0f);       
        glColor3ub(245, 245, 220); //beige
        glEnable(GL_TEXTURE_2D);   //texture
        glBindTexture(GL_TEXTURE_2D, tob[1]); //paper
    }
    else if(rand_num==3) {
        glClearColor(0.0f, 0.0f, 0.0f, 1.0f);   
        glColor3ub(245, 245, 220); //beige
        glEnable(GL_TEXTURE_2D);   //texture
        glBindTexture(GL_TEXTURE_2D, tob[2]); //scissors    
    }
    else if(rand_num==4) {
        glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
        glDisable(GL_TEXTURE_2D); //disable texture
        glColor3ub(255,0,0); //red
    }
    else if(rand_num==5) {
        glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
        glDisable(GL_TEXTURE_2D); //disable texture
        glColor3ub(0,0,255); //blue 
    }
}


//for the second part of program 
void display_game()
{



    glClearColor(0.0f, 0.0f, 0.0f, 1.0f); //Black and opaque
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    //define the projection matrix just once and use the modelview matrix all other times

    glMatrixMode(GL_PROJECTION);  //Applies subsequent matrix operations to the projection matrix stack
    glLoadIdentity();//Reset

    GLint viewport[4];
    glGetIntegerv(GL_VIEWPORT, viewport); //The params parameter returns four values: the x and y window coordinates of the viewport, followed by its width and height
    double aspect = (double)viewport[2] / (double)viewport[3]; // y/width would be 1.0


    gluPerspective(60.0f,aspect, 1.0f, 100.0f); //using perspective projection
    //glOrtho ((float)60/(float)60, (float)-60/(float)60, -1, 1, 0.8, 100);


    glMatrixMode(GL_MODELVIEW); //for trasformations - Applies subsequent matrix operations to the texture matrix stack
    glLoadIdentity();

    // move back a bit for viewer  , cause of gluPerspective
    glTranslatef( 0, 0, -35 );


    float e=0,f=0;

    //SEED to some constant value for 2nd part of the program. If it is not used , cubes would change color in runtime
    //set it before the design begins , to avoid flickering
    srand(0x3423449);   



    //Important
    //glEnable(GL_TEXTURE_2D);

    int choice;
    //choice=myRandom();

    // Set the camera
    gluLookAt(  x, 1.0f, z, //camera
            x+lx, 1.0f,  z+lz, //eye
            0.0f, 1.0f,  0.0f); //vector-not tilted camera

    //construct the grid with reference the central cube  
    for(int i=0;i<8;i++) {
        for(int j=0;j<8;j++) {  

            choice=myRandom();
            handle_myRandom(choice);
            glPushMatrix();
                glTranslatef(0.0f+e,0.0f+f,0.0f); //right and below
                glRotatef(20.0f,1.0f,-2.0f,0.0f); //looking 3d
                    myglutSolidCube(2.25);
                glPopMatrix();

            choice=myRandom();
            handle_myRandom(choice);
            glPushMatrix();
                glTranslatef(0.0f-e,0.0f+f,0.0f); //left and below
                glRotatef(20.0f,1.0f,-2.0f,0.0f); //looking 3d
                    myglutSolidCube(2.25);
                glPopMatrix();

            choice=myRandom();
            handle_myRandom(choice);
            glPushMatrix();
                glTranslatef(0.0f+e,0.0f-f,0.0f); //right and up
                glRotatef(20.0f,1.0f,-2.0f,0.0f); //looking 3d
                    myglutSolidCube(2.25);
                glPopMatrix();


            choice=myRandom();
            handle_myRandom(choice);
            glPushMatrix();
                glTranslatef(0.0f-e,0.0f-f,0.0f); //left and up
                glRotatef(20.0f,1.0f,-2.0f,0.0f); //looking 3d
                    myglutSolidCube(2.25);
                glPopMatrix();


                f += -2.63;
            }
            f=0;
            e+=2.63;
        }



    //glPushMatrix();

    //glDepthMask(GL_FALSE);

    //glTranslated(ox, oy, oz);
    //int pinakas[2][2];



    //printf("(x, y, z) in world coordinates is:(%d, %d, %d)\n", (int)ox, (int)oy, (int)oz);

    //pinakas[0][0]=ox;
    //pinakas[0][1]=oy;

    //printf("arxiko x %d\n",pinakas[0][0]);
    //printf("arxiko y %d\n",pinakas[0][1]);

    glutSwapBuffers(); //implicit   glFlush



    glDisable(GL_TEXTURE_2D);



}


void reshape(GLsizei width, GLsizei height) { 

    // GLsizei for non-negative integer // Compute aspect ratio of the new window

    if (height == 0) height = 1; // To prevent divide by 0 
    GLfloat aspect = (GLfloat)width / (GLfloat)height; // Set the viewport to cover the new window 
    glViewport(0, 0, width, height); // Set the aspect ratio of the clipping volume                 glMatrixMode(GL_PROJECTION); // To operate on the Projection matrix 
    glLoadIdentity(); // Reset // Enable perspective projection with fovy, aspect, zNear and zFar 
    gluPerspective(45.0f, aspect, 0.1f, 100.0f); 

} 

void timer(int extra)
{
    glutPostRedisplay();
    glutTimerFunc(16, timer, 0);
}


void mouseEscape( int button, int state, int x, int y )
{
    if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN &&button==MENU_EXIT)

    {
        int windowID = glutCreateWindow("CUBES");
        glutDestroyWindow(windowID);
        exit(0);
    }

            glutPostRedisplay();

}

void processSpecialKeys(int key, int xx, int yy) {

    float fraction = 1.5f;

    switch (key) {
        case GLUT_KEY_LEFT :
            angle -= 0.01f;
            lx = sin(angle);
            lz = -cos(angle);
            break;
            case GLUT_KEY_RIGHT :
            angle += 0.01f;
            lx = sin(angle);
            lz = -cos(angle);
            break;
        case GLUT_KEY_UP :
            x += lx * fraction;
            z += lz * fraction;
            break;
        case GLUT_KEY_DOWN :
            x -= lx * fraction;
            z -= lz * fraction;
            break;
    }
}

int main(int argc, char **argv)
{
    glutInit(&argc, argv);
    glutInitWindowPosition(100,100);
        glutInitWindowSize(600,600);
        glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE| GLUT_MULTISAMPLE);
        glEnable(GL_MULTISAMPLE); //anti-alliasing  
    glutCreateWindow("CUBES");



    //create and handle the menu
    my_createmenu();

    //srand(time(NULL));



    glutDisplayFunc(display);
    glutReshapeFunc(reshape);

    //glutMouseFunc(mouse_handler);
    //glutTimerFunc(0, timer, 0);
    //glutSpecialFunc(processSpecialKeys);

        init();
    //glEnable(GL_TEXTURE_2D); //for texture
    //glutMouseFunc(mouseEscape);
    //glutKeyboardFunc(keyEscape);



    //textures

    //ffffffffffffffffffffffffffffffffffff
    glGenTextures(3, &tob[0]);

        loadTextureFromFile(filename1, tob[0]);
        loadTextureFromFile(filename2, tob[1]);
        loadTextureFromFile(filename3, tob[2]); 

    glutSpecialFunc(processSpecialKeys);
    //Load our texture
    //loadTextureFromFile(filename);

    glutMainLoop();

    //Free our texture
    //FreeTexture(tob);

    return 0;
}

//create the menu-entries
void my_createmenu(void) {

    // Create a menu
        glutCreateMenu(menu);

        // Add menu items
        glutAddMenuEntry("Start Game", MENU_START);
        glutAddMenuEntry("Exit", MENU_EXIT);

        // Associate a mouse button with menu
        glutAttachMenu(GLUT_RIGHT_BUTTON);


}


// Menu handling function-what to do in each value
void menu(int item)
{
        switch (item)
        {
        case MENU_START: {

        //glEnable(GL_TEXTURE_2D); //for texture
        //Load our texture
        //glGenTextures(3ss, &tob[0]);
            //loadTextureFromFile(filename1, tob[0]);
            //loadTextureFromFile(filename2, tob[1]);
            //loadTextureFromFile(filename3, tob[2]);   

        glutDisplayFunc(display_game);


        }
        break;
        case MENU_EXIT: 
        {
        int windowID = glutCreateWindow("CUBES"); //exit game
        glutDestroyWindow(windowID);
        exit(0);                
            }
                break;  
        default:
                {       /* Nothing */       }
                break;
        }

        glutPostRedisplay();

        return;
}

我在这里使用eval是因为我计划在多个组件之间重用此功能以切换其本地状态。

这些是组件:

togglePlaceholder = (nodeType:string,localStateValue:string) => {
    switch (eval(`this.state.${localStateValue}`)) {
        case nodeType:
            return this.setState({[localStateValue]:null});
        case null:
            return this.setState({[localStateValue]:nodeType});
    }
}

似乎可以正常登录,但不能正常登录。

1 个答案:

答案 0 :(得分:0)

鉴于您只是隐藏或显示我想您可以使用一些思考的枢纽来解决这个问题:)

在我看来,最简单的方法是跟踪当前关注的元素的id并使用它有条件地呈现占位符:

<TextInput onFocus={() => setActive(id)} onBlur={clearActive} placeholder={this.state.activeId === id ? 'placeholder' : ''} />

创建一个HoC来管理其中的输入,让您进行相应的分组很简单