Reactjs 如何使用context.state设置组件状态?

Reactjs 如何使用context.state设置组件状态?,reactjs,Reactjs,我最近遇到了一个与React上下文有关的问题 我在localstorage中存储了一些数据,我只打算在上下文提供程序没有可用数据时使用这些数据 localstorage中的数据存储在我的组件状态中。 如果有来自上下文的可用数据,我希望重写此.state 困难在于,当上下文只能在render方法中使用时,我不知道如何设置状态。 在渲染中调用setState是一种非常糟糕的做法,我不知道如何在渲染之外获取context.state 如果没有来自的数据 下面是一些示例代码 欢迎提出任何让我更接近解决方

我最近遇到了一个与React上下文有关的问题

我在localstorage中存储了一些数据,我只打算在上下文提供程序没有可用数据时使用这些数据

localstorage中的数据存储在我的组件状态中。 如果有来自上下文的可用数据,我希望重写此.state

困难在于,当上下文只能在render方法中使用时,我不知道如何设置状态。 在渲染中调用setState是一种非常糟糕的做法,我不知道如何在渲染之外获取context.state

如果没有来自的数据

下面是一些示例代码

欢迎提出任何让我更接近解决方案的想法

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

componentDidMount() {
    this.setState({
        data: localstorage.getItem('data')
    })
}

render() {
  return (
    <>
      <AppContext.Consumer>
          {context => (
              <>
                 {typeof context.state.data !== 'undefined'&&
                     <div>
                         {/*Print out data from this.state or from context.state*/}
                     </div>
                 }
              </>
          )}
      </AppContext.Consumer>
    </>
  )
}
构造函数(道具){
超级(道具);
此.state={
数据:“”
}
}
componentDidMount(){
这是我的国家({
数据:localstorage.getItem('data')
})
}
render(){
返回(
{context=>(
{typeof context.state.data!==“未定义”&&
{/*从this.state或context.state打印数据*/}
}
)}
)
}

您可以使用以下技巧访问渲染外部的上下文:

import { PageTitleContext } from '../lib/pageTitleProvider';
import { Helmet } from 'react-helmet';
import * as PropTypes from 'prop-types';

class PageTitle extends Component {
  componentDidMount() {
    if (this.props.title)
      this.props.context.setTitle(this.props.title);
  }

  componentDidUpdate(prevProps, prevState, snapshot) {
    if (this.props.title !== prevProps.title)
      this.props.context.setTitle(this.props.title);
  }

  render() {
    if (this.props.title) {
      return <Helmet>
        <title>{this.props.context.title}</title>
      </Helmet>;
    }

    if (!this.props.title) {
      return this.props.context.title;
    }
  }
}

PageTitle.propTypes = {
  title: PropTypes.string,
  context: PropTypes.object.isRequired,
};

export default (props) => (
  <PageTitleContext.Consumer>
    {(context) => <PageTitle {...props} context={context}/>}
  </PageTitleContext.Consumer>
)
从“../lib/pageTitleProvider”导入{PageTitleContext};
从“react Helmet”导入{Helmet};
从“道具类型”导入*作为道具类型;
类PageTitle扩展组件{
componentDidMount(){
如果(本道具标题)
this.props.context.setTitle(this.props.title);
}
componentDidUpdate(prevProps、prevState、快照){
if(this.props.title!==prevProps.title)
this.props.context.setTitle(this.props.title);
}
render(){
如果(本道具标题){
返回
{this.props.context.title}
;
}
如果(!this.props.title){
返回this.props.context.title;
}
}
}
PageTitle.propTypes={
标题:PropTypes.string,
上下文:PropTypes.object.isRequired,
};
导出默认值(道具)=>(
{(上下文)=>}
)

您可以使用以下技巧访问渲染外部的上下文:

import { PageTitleContext } from '../lib/pageTitleProvider';
import { Helmet } from 'react-helmet';
import * as PropTypes from 'prop-types';

class PageTitle extends Component {
  componentDidMount() {
    if (this.props.title)
      this.props.context.setTitle(this.props.title);
  }

  componentDidUpdate(prevProps, prevState, snapshot) {
    if (this.props.title !== prevProps.title)
      this.props.context.setTitle(this.props.title);
  }

  render() {
    if (this.props.title) {
      return <Helmet>
        <title>{this.props.context.title}</title>
      </Helmet>;
    }

    if (!this.props.title) {
      return this.props.context.title;
    }
  }
}

PageTitle.propTypes = {
  title: PropTypes.string,
  context: PropTypes.object.isRequired,
};

export default (props) => (
  <PageTitleContext.Consumer>
    {(context) => <PageTitle {...props} context={context}/>}
  </PageTitleContext.Consumer>
)
从“../lib/pageTitleProvider”导入{PageTitleContext};
从“react Helmet”导入{Helmet};
从“道具类型”导入*作为道具类型;
类PageTitle扩展组件{
componentDidMount(){
如果(本道具标题)
this.props.context.setTitle(this.props.title);
}
componentDidUpdate(prevProps、prevState、快照){
if(this.props.title!==prevProps.title)
this.props.context.setTitle(this.props.title);
}
render(){
如果(本道具标题){
返回
{this.props.context.title}
;
}
如果(!this.props.title){
返回this.props.context.title;
}
}
}
PageTitle.propTypes={
标题:PropTypes.string,
上下文:PropTypes.object.isRequired,
};
导出默认值(道具)=>(
{(上下文)=>}
)

谢谢您的回答@landorid。非常好的解决方案!谢谢你的回答@landorid。非常好的解决方案!