最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How do I setState within React's recompose's lifecycle method? - Stack Overflow

programmeradmin1浏览0评论

I am using recompose in my React project /

It's a great library. I'm using the compose utility as a container component that passes state down as props to the presentational component like so:

const enhance = compose(
  lifecycle({
    componentDidMount() {
      myCall
        .getResponse([productId])
        .then(products => {
          setIsReady(true);
        });
    },
  }),
  withState('isAvailable', 'setIsAvailable', false),
  withState('isReady', 'setIsReady', false),
  mapProps(({
    setIsAvailable,
    setIsReady,
    ...state,
  }) => ({
    onLogoutTouchTap: () => {
      ...

Note the setIsReady(true) call within componentDidMount. This is what I want to do, however lifecycle/componentDidMount doesn't have access to setIsReady. How can I accomplish my intended result of updating state from componentDidMount with recompose?

I am using recompose in my React project https://github.com/acdlite/recompose/

It's a great library. I'm using the compose utility as a container component that passes state down as props to the presentational component like so:

const enhance = compose(
  lifecycle({
    componentDidMount() {
      myCall
        .getResponse([productId])
        .then(products => {
          setIsReady(true);
        });
    },
  }),
  withState('isAvailable', 'setIsAvailable', false),
  withState('isReady', 'setIsReady', false),
  mapProps(({
    setIsAvailable,
    setIsReady,
    ...state,
  }) => ({
    onLogoutTouchTap: () => {
      ...

Note the setIsReady(true) call within componentDidMount. This is what I want to do, however lifecycle/componentDidMount doesn't have access to setIsReady. How can I accomplish my intended result of updating state from componentDidMount with recompose?

Share Improve this question asked Jan 14, 2017 at 14:53 Mark Shust at M.academyMark Shust at M.academy 6,4294 gold badges34 silver badges51 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 18

Well I found out if you move the lifecycle method after the withState methods, you have access to the setters by accessing this.props.setterFunction. In my case, here is the solution I was looking for:

const enhance = compose(
  withState('isAvailable', 'setIsAvailable', false),
  withState('isReady', 'setIsReady', false),
  lifecycle({
    componentDidMount() {
      myCall
        .getResponse([productId])
        .then(products => {
          this.props.setIsReady(true);
        });
    },
  }),
  mapProps(({
    setIsAvailable,
    setIsReady,
    ...state,
  }) => ({
    onLogoutTouchTap: () => {
      ...
发布评论

评论列表(0)

  1. 暂无评论