2017-12-10 15 views
2

レンダリング機能が提供されているにもかかわらず、JSXを直接返す代わりにレンダリングが行われない理由を理解することに問題があります。この場合、市のレンダリング機能が提供されているときに小道具を再レンダリングしないでください

export default React => { 
    const InputBox = ({ city, setCity, setTime }) => { 
    const onInputEnter = createOnInputEnter({ city, setCity, setTime, getCityTime }) 

    return (
     <div className='InputBox'> 
      <input 
      defaultValue={city} 
      onKeyPress={onInputEnter} 
      onFocus={moveFocusAtEnd} 
      autoFocus /> 
     </div> 
    ) 
    } 

    return InputBox 
} 

私は街のReduxの状態を変更するたびに再レンダリングされます。

はここでInputBox関数コンポーネントの私の例です。

export default React => { 
    const InputBox = ({ city, setCity, setTime }) => { 
    const onInputEnter = createOnInputEnter({ city, setCity, setTime, getCityTime }) 

    const componentDidMount = async() => { 
     const { city: resultCity, time: resultTime } = await getCityTime(city) 

     setCity(resultCity) 
     setTime(resultTime) 
    } 

    const render =() => { 
     return (
     <div className='InputBox'> 
      <input 
      defaultValue={city} 
      onKeyPress={onInputEnter} 
      onFocus={moveFocusAtEnd} 
      autoFocus /> 
     </div> 
    ) 
    } 

    return { render, componentDidMount } 
    } 

    return InputBox 
} 

この例では、都市は1回だけ渡され、還元状態が変化すると再レンダリングされません。

初期化にcomponentDidMountメソッドを使用しているので、後者の例を使用したいと思います。

この例では、ReactとReact-Reduxを使用しています。私のInputBox関数のコンテナがどのように見えるか

は、これは次のとおりです。

import createInputBox from '../components/InputBox.js' 
import { connect } from 'react-redux' 

... 

export default React => { 
    const InputBox = createInputBox(React) 

    const InputBoxWithReduxStore = connect(
    ({ city }) => ({ city }), 
    mapDispatchToProps 
)(InputBox) 

    return InputBoxWithReduxStore 
} 

EDIT:

function createOnInputEnter ({ city, setCity, setTime, getCityTime }) { 
    return async ({ key, target }) => { 
    if (key === 'Enter') { 
     try { 
     const inputCity = target.value 

     setTime('...') 

     const { city: resultCity, time: resultTime } = await getCityTime(inputCity) 

     setCity(resultCity) 
     setTime(resultTime) 
     } catch (err) { 
     const { city: resultCity, time: resultTime } = await getCityTime(city) 

     setCity(resultCity) 
     setTime(resultTime) 
     } 
    } 
    } 
} 

@zarcodeが反応使用して示唆したように、私はcreateOnInputEnter機能の実装を表示するためのコードスニペットを含めています

.Component、それは私の要件に合っていません。私はClass-lessコンポーネントを使用しているからです。ミックスインの詳細:https://gist.github.com/jquense/47bbd2613e0b03d7e51c

+0

createOnInputEnter機能のcatch節で –

+0

@RIYAJKHAN https://codesandbox.io/s/8kvozyw682 –

+0

@PeterParadaでそれを追加してください - あなたはそれを還元状態で変更した後も、都市はまだデフォルトです。 –

答えて

1

componentDidMountは一度だけ呼び出され、ボトムアップでも動作します。 すべての状態のレンダリングでは、componentWillReceievepropsを使用することができます。

+0

このソリューションの問題点は、更新された小道具にアクセスする必要があることです。componentWillReceivePropsの外側にあるcreateOnInputEnter関数の内部からのcityです。たぶん私は間違っている、あなたは私がcreateOnInputEnter関数内の更新された都市のプロパティにアクセスすることでこの問題を解決する方法を示す実例を教えてくれますか?この関数の実装を示すコードを編集します。 –

+0

また、componentDidMountは一度だけ呼び出されるので、私は初期化のためだけに使用するため、一度だけ呼び出されることが予想されます。 –

1

最初の例は機能コンポーネントです。コンポーネントライフサイクルメソッド(componentDidMountなど)が不要な場合に使用できます。

2番目の例は、機能コンポーネントとライフサイクルメソッドが混在していて、うまく動作せず、試したことがないかどうかはわかりません。 代わりに、ライフサイクルメソッドをコンポーネントで使用する場合は、React.Componentを拡張するクラスコンポーネントを使用することをお勧めします。最初のケースconst onInputEnter = createOnInputEnter({ city, setCity, setTime, getCityTime })

... 
import {Component} from "react"; 

export default class InputBox extends Component { 
    onInputEnter =() => { 
     const { city, setCity, setTime } = this.props; 
     createOnInputEnter({ city, setCity, setTime, getCityTime }) 
    } 

    async componentDidMount() { 
    const { city, setCity, setTime } = this.props; 
    const { city: resultCity, time: resultTime } = await getCityTime(city) 

    setCity(resultCity) 
    setTime(resultTime) 
    } 

    render(){ 
    return (
     <div className='InputBox'> 
     <input 
      defaultValue={city} 
      onKeyPress={onInputEnter} 
      onFocus={moveFocusAtEnd} 
      autoFocus /> 
     </div> 
    ) 
    } 

}

+0

コード例をありがとう、しかし、私は非クラスのReactを書く。 https://github.com/ericelliott/react-pure-component-starterとMixinレシピ:https://gist.github.com/jquense/47bbd2613e0b03d7e51cからインスパイアされています。 –

0

それが機能コンポーネントであるコンポーネントのrenderメソッド内にある:それは次のようになります。

私はあなたがこのような何かをする必要があります最初のものと同じであることをどのようにこのコンセプトの作品が、第二の例のためになっていない午前:

export default React => { 
    const InputBox = ({ city, setCity, setTime }) => { 

    const componentDidMount = async() => { 
     const { city: resultCity, time: resultTime } = await getCityTime(city) 

     setCity(resultCity) 
     setTime(resultTime) 
    } 

    const render =() => { 
    const { city, setCity, setTime } = this.props; 
    const onInputEnter = createOnInputEnter({ city, setCity, setTime, getCityTime }) 
     return (
     <div className='InputBox'> 
      <input 
      defaultValue={city} 
      onKeyPress={onInputEnter} 
      onFocus={moveFocusAtEnd} 
      autoFocus /> 
     </div> 
    ) 
    } 

    return { render, componentDidMount } 
    } 

    return InputBox 
} 

私は、これは仕方がアクセスするかどうかわからないのですが、内部の小道具がここにレンダリングされます。

関連する問題