简体   繁体   English

如何在 ComponentWillMount 中使用 window.location.href?

[英]How to use window.location.href in ComponentWillMount?

I want to update state via ComponentWillMount() like:我想通过ComponentWillMount()更新状态,例如:

this.state = {
  Canonical:''
}
componentWillMount(){
  this.setState({Canonical: window.location.href})
}

Anybody have idea how to do that?有人知道怎么做吗?

You should use componentDidMount method.您应该使用componentDidMount方法。 This lifecycle method is invoked after the mounting of component and the same time it invokes componentWillMount .此生命周期方法在组件安装后调用,同时调用componentWillMount

setting state re-renders the component.设置状态重新渲染组件。

Another option is you initialise the state value in constructor.另一种选择是在构造函数中初始化状态值。

 constructor(){
      super();
      this.state = {
        Canonical: window.location.href
      }
    }

or或者

componentDidMount(){
  this.setState({Canonical: window.location.href})
} 

You should avoid async initialization in componentWillMount()您应该避免在componentWillMount()异步初始化

componentWillMount() is invoked immediately before mounting occurs. componentWillMount()在挂载发生前立即被调用。 It is called before render() , therefore setting state in this method will not trigger a re-render.它在render()之前调用,因此在此方法中设置状态不会触发重新渲染。 Avoid introducing any side-effects or subscriptions in this method.避免在此方法中引入任何副作用或订阅。

Make async calls for component initialization in componentDidMount instead of componentWillMountcomponentDidMount而不是componentWillMount对组件初始化进行异步调用

function componentDidMount() {
  axios.get(`api/messages`)
    .then((result) => {
      const messages = result.data
      console.log("COMPONENT WILL Mount messages : ", messages);
      this.setState({
        messages: [...messages.content]
      })
    })
}

Note that componentWillMount is depreciated because it is not safe for async rendering请注意, componentWillMount已折旧,因为它对于异步渲染不安全

React: https://reactjs.org/docs/react-component.html#unsafe_componentwillmount反应: https : //reactjs.org/docs/react-component.html#unsafe_componentwillmount

Good read: https://medium.com/@baphemot/whats-new-in-react-16-3-d2c9b7b6193b好读: https : //medium.com/@baphemot/whats-new-in-react-16-3-d2c9b7b6193b

If you want to setState while initializing then如果你想在初始化时设置状态然后

constructor(props){
  super(props);
  this.state = {
    Canonical : window.location.href
  };
}

or if you want to update the state on componentWillMount then或者如果你想更新 componentWillMount 上的状态,那么

constructor(props){
   super(props);
   this.state = {
     Canonical : ''
   };
}

componentWillMount(){
  this.setState({ Canonical : window.location.href });
}

componentWillMount function will call before the render function. componentWillMount 函数将在渲染函数之前调用。 If something went wrong in componentWillMount the component won't render.如果 componentWillMount 出现问题,组件将不会呈现。 it will block better you can try with componentDidMount();它会更好地阻止你可以尝试使用 componentDidMount(); function this function will be called after the render function in the component function 该函数将在组件中的 render 函数之后调用

Class Component{
  //sample structure of component and the flow
  componentWillMount(){}
  render(){}
  componentDidMount(){}
}

If you are using React Router then you can call it like : this.props.location its a feature in that package to know more : https://reacttraining.com/react-router/web/api/location如果您使用的是React Router,那么您可以这样称呼它: this.props.location它是该包中的一个功能以了解更多信息: https : this.props.location

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM