javascript - ReactJS 无法通过 onClick 重新渲染组件

标签 javascript reactjs

我用 react.js 制作了一个简单的应用程序,每次点击按钮时建议随机放置 问题点击后无法重新渲染组件 每次点击按钮我怎样才能找到新的地方

place.js where fetch API获取数据

state = { loading: true, person: null };
  async componentDidMount() {
    let Xyurl = "https://cors-anywhere.herokuapp.com/";
    let url =
      "https://wainnakel.com/api/v1/GenerateFS.php?uid=26.2716025,50.2017993&g et_param=value";

    let response = await fetch(Xyurl + url);
    let data = await response.json();
    this.setState({ place: data, loading: false });

  }
  render() {
    return (
      <div>
        <div>
          {this.state.loading || !this.state.place ? (
            <div>loading......</div>
          ) : (
            <div className="PlcseContiner">
              <img alt="description" src= . 
              {this.state.place.image[0]} />
                {this.state.place.name}

        </div>
      </div>
    );

建议按钮在哪里可以渲染地方

class Suggestion extends Component {
  constructor(props) {
    super(props);
    this.state = {
      visible: false
    };
    this.handleClicked = this.handleClicked.bind(this);
  }
  handleClicked(event) {
    event.preventDefault();
    this.setState({
      visible: true
    });
  }

  generateplace() {
    if (this.state.visible) {
      return <Place />;
    }
    return null;
  }

  render() {
    return (
      <div className="main-button">
        <Button onClick={this.handleClicked} color="warning" className="Btn">
          suggestion
        </Button>
        {this.generateplace()}
      </div>
    );
  }

最佳答案

您可以使用 componentWillUpdate 从父级控制它 所以你需要在 Place 组件中添加 props

建议:

class Suggestion extends Component {
  constructor(props) {
    super(props);
    this.state = {
      visible: false,
      refresh: 1
    };
    this.handleClicked = this.handleClicked.bind(this);
  }
  handleClicked(event) {
    event.preventDefault();
    this.setState({
      visible: true,
      refresh: this.state.refresh + 1
    });
  }

  generateplace() {
    if (this.state.visible) {
      return <Place refresh={this.state.refresh} />;
    }
    return null;
  }

  render() {
    return (
      <div className="main-button">
        <Button onClick={this.handleClicked} color="warning" className="Btn">
          suggestion
        </Button>
        {this.generateplace()}
      </div>
    );
  }

您需要像这样将操作分离到函数中:

地点:

componentDidMount() {
    this.getPlace()
}

componentWillUpdate(nextProps, nextState) {
    this.getPlace();
}

getPlace = async() => {
    let Xyurl = "https://cors-anywhere.herokuapp.com/";
    let url =
          "https://wainnakel.com/api/v1/GenerateFS.php?uid=26.2716025,50.2017993&g et_param=value";

    let response = await fetch(Xyurl + url);
    let data = await response.json();
    this.setState({ place: data, loading: false });
}

关于javascript - ReactJS 无法通过 onClick 重新渲染组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56331077/

相关文章:

javascript - 从 Google map 加载 div 中的确切位置

javascript - 可折叠在控制组内

javascript - HTML 中的多个点击

javascript - React Native - 不变违规 : Objects are not valid as a React child

reactjs - 返回 JSX 的函数和类组件有什么区别?

javascript - 多次订阅 Observable 与订阅一次并共享结果

javascript - 使用 CSRF token 登录 Cypress

javascript - react-native-really-awesome-button onPress 在状态改变时不更新功能

javascript - 煎锅过滤器组件定制

ReactJS:返回渲染多个对象