反应:按钮无效/无法执行状态更新

时间:2019-06-25 21:13:36

标签: reactjs button

我写了以下网站:https://konekto-hgol6b5mz.now.sh

如果您快速单击入门和设置,您会看到我在“ Direct SOS”按钮上有一个屏幕。但是,它的功能没有执行,因此我不会被重定向。在这里,您可以看到与此相关的课程:

import React from 'react';
import { Button, Grid } from '@material-ui/core';
import { withStyles } from '@material-ui/core/styles';
import AppContext from '../utils/AppContext';

const styles = theme => ({
  container: {
    alignItems: 'center',
    // background: 'white',
    border: 'black',
    'border-width': 'medium',
    'margin-top': '80px',
    background: 'rgba(255, 255, 255, 0.8)',
    'border-radius': '20px'
  },
  item: {
    // background: 'red',
    width: '100%',
    //background: 'white',
    'text-align': 'center',
    'border-radius': '5px',
    'margin-top': '10px'
  },
  sosbutton: {
    background: 'red',
    'text-align': 'center',
    'margin-top': '30px',
    height: '80%',
    width: '100%'
  }
});

class Landingpage extends React.Component {
  static contextType = AppContext;

  constructor(props) {
    super(props);
    this.classes = props.classes;
    this.state = {};
    this.handleDirectSOS = this.handleDirectSOS.bind(this);
  }

  componentDidMount() {
    console.log(this.context);

    if (this.context.onBoardingStatus === false) {
      console.log('IN IF');
      this.props.history.push('/onboarding');
    }
  }
  handleDirectSOS() {
    console.log('direct SOS');
    this.props.history.push('/emergency_sent');
  }
  render() {
    console.log('direct SOS');
    return (
      <Header title="Send out SOS" />
      <Grid
        container
        className={this.classes.container}
        direction="column"
        spacing={2}
      >
        <Grid
          item
          sm={12}
          className={(this.classes.item, this.classes.forwardbutton)}
        >
          <Button
            className={this.classes.sosbutton}
            name="type_person"
            value="1"
            onClick={this.props.handleDirectSOS}
          >
            Direct SOS
          </Button>
        </Grid>
      </Grid>
    );
  }
}

export default withStyles(styles)(Landingpage);
  

index.js:1375警告:无法在服务器上执行React状态更新   未安装的组件。这是空操作,但表示内存泄漏   在您的应用程序中。要修复,请取消所有订阅并异步   componentWillUnmount方法中的任务。       在“设置”中(由WithStyles(Settings)创建)       在WithStyles(Settings)中(由Context.Consumer创建)

这可能与“设置”文件相关联,但我不知道可能是什么原因。在这里您可以看到设置文件:

import React from 'react';
import axios from 'axios';
import {
  Grid,
  Box,
  Container,
  Typography,
  Button,
  TextField
} from '@material-ui/core';

import { withStyles } from '@material-ui/core/styles';
import Header from '../Layout/Header';

import CONST from '../utils/Constants';

const CssTextField = withStyles({
  root: {
    '& label.Mui-focused': {
      color: 'green'
    },
    '& .MuiInput-underline:after': {
      borderBottomColor: 'green'
    },
    '& .MuiOutlinedInput-root': {
      '& fieldset': {
        borderColor: 'red'
      },
      '&:hover fieldset': {
        borderColor: 'yellow'
      },
      '&.Mui-focused fieldset': {
        borderColor: 'green'
      }
    }
  },
  layout: {
    width: '100%'
  }
})(TextField);

const styles = theme => ({
  root: {
    display: 'flex',
    flexWrap: 'wrap',
    width: '100%'
  },
  title: {
    'text-align': 'center'
  },
  textfield: {
    'margin-top': theme.spacing(1),
    'margin-bottom': theme.spacing(2)
  }
});

//const classes = useStyles();

class Settings extends React.Component {
  constructor(props) {
    super(props);
    //const { classes } = props;
    this.classes = props.classes;
    this.state = {};
    this.onChangeText = this.onChangeText.bind(this);
    this.onSubmit = this.onSubmit.bind(this);
  }

  onChangeText(e) {
    console.log('text has changed.');
    const key = e.target.id;
    const value = e.target.value;
    let state_obj = {};
    state_obj[key] = value;
    this.setState(state_obj);
  }

  onSubmit(e) {
    console.log('Submit button pressed.');
    axios
      .post(CONST.URL + 'user/update', {
        id: 1,
        data: this.state
      })
      .then(res => {
        console.log(res);
        console.log(res.data);
      })
      .catch(err => {
        console.log(err);
      });
    this.props.history.push('/');
  }

  componentDidMount() {
    console.log('Component did mount.');
    axios
      .get(CONST.URL + 'user', {
        params: { id: 1 }
      })
      .then(resp => {
        // console.log(resp);
        const data = resp.data.data;
        this.setState(data);
        console.log(this.state.fullname);
      })
      .catch(function(error) {
        console.log(error);
      })
      .then(function() {
        // always executed
      });
  }

  render() {
    return (
      <React.Fragment>
        <Header title="Settings" BackButton="true" />
        <Container component="main" maxWidth="sm">
          {/* <Typography variant="h4" align="center" gutterBottom="true">
            Settings
          </Typography> */}
          <Box className={this.classes.textfield}>
            <Grid
              container
              direction="column"
              justify="flex-end"
              alignItems="left"
              item
              xs
            >
              <Typography variant="h6">Personal Information</Typography>
              <CssTextField
                id="fullname"
                label="Fullname"
                onChange={this.onChangeText}
                value={this.state.fullname}
              />
              <CssTextField
                id="birthday"
                label="Birthday"
                onChange={this.onChangeText}
                value={this.state.birthday}
              />
              <CssTextField
                id="address"
                label="Home address"
                onChange={this.onChangeText}
                value={this.state.address}
              />
            </Grid>
          </Box>
          <Box className={this.classes.textfield}>
            <Grid
              container
              direction="column"
              justify="flex-end"
              alignItems="left"
              item
              xs
            >
              <Typography variant="h6">Health information</Typography>
              <CssTextField
                id="allergies"
                label="Allergies"
                onChange={this.onChangeText}
                value={this.state.allergies}
              />
              <CssTextField
                id="past_injuries"
                label="Past injuries"
                onChange={this.onChangeText}
                value={this.state.past_injuries}
              />
            </Grid>
          </Box>
          <Box className={this.classes.textfield}>
            <Grid
              container
              direction="column"
              justify="flex-end"
              alignItems="left"
              item
              xs
            >
              <Typography variant="h6">Contact information</Typography>
              <CssTextField
                id="fullname_relative_1"
                label="Fullname relative 1"
                onChange={this.onChangeText}
                value={this.state.fullname_relative_1}
              />
              <CssTextField
                id="phone_number_relative_1"
                label="Phone number relative 1"
                onChange={this.onChangeText}
                value={this.state.phone_number_relative_1}
              />
              <CssTextField
                id="fullname_relative_2"
                label="Fullname relative 2"
                onChange={this.onChangeText}
                value={this.state.fullname_relative_2}
              />
              <CssTextField
                id="phone_number_relative_2"
                label="Phone number relative 2"
                onChange={this.onChangeText}
                value={this.state.phone_number_relative_2}
              />
            </Grid>
          </Box>
          <Box>
            <Grid
              container
              direction="column"
              justify="flex-end"
              alignItems="left"
              item
              xs
            >
              <Button
                variant="contained"
                className={this.classes.button}
                onClick={this.onSubmit}
              >
                Save
              </Button>
              <br />
              {/* <Button
                variant="contained"
                className={this.classes.button}
                onClick={() => {
                  this.props.history.push('/');
                }}
              >
                Cancel emergency
              </Button> */}
              {/* <br /> */}
              <Button
                variant="contained"
                className={this.classes.button}
                onClick={() => {
                  this.props.history.push('/onboarding_reset');
                }}
              >
                Reset App
              </Button>
              {/* <br />
              <Button
                variant="contained"
                className={this.classes.button}
                onClick={() => {
                  this.props.history.push('/Signin');
                }}
              >
                Signin
              </Button> */}
            </Grid>
          </Box>
        </Container>
      </React.Fragment>
    );
  }
}

export default withStyles(styles)(Settings);

非常感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

我认为您有两个不同的问题。

按钮按下问题是因为您正在呼叫onClick={this.props.handleDirectSOS}而不是onClick={this.handleDirectSOS}

您看到的错误是由于行this.setState(data);引起的,如果组件已卸载,则需要对其进行包装或取消调用。有很多类似Is there a way to check if the react component is unmounted?

的文章
相关问题