나는 Refact가 & Redux 인 날씨 앱을 구축 중입니다. 나는 반응하지 않는 멍청이로서 미심쩍은 물로 탐험하기로 결정했다. & Redux. 저는 프레젠테이션 구성 요소와 데이터를 처리 할 각각의 컨테이너로 물건을 분할하고 있습니다. 이 문제를 해결하기 위해 몇 가지 문제가 있습니다. 내가 그것을 어떻게하려고하는지 나는 정말로 확신 할 수 없다.React + Redux : 프리젠 테이션을 데이터에서 분리하기
는 지금은 SearchBar
, CurrentWeather
, & Forecast
구성 요소와 나는에 해당 구성 요소를 통합하기 위해 노력하고있어 AppContainer
있습니다. 지금까지 AppContainer
에 SearchBar
구성 요소가 통합되어 있으며 문제없이 작동합니다. 여기 내가 혼란스러워하는 곳이있다. 그래서 나는 컨테이너에 필요한 액션과 컴포넌트를 제공했고 컨테이너는 connected
이었습니다. 그래서 사용자가 검색을하면 API 호출이 만들어지고 상태는 감속기를 통해 업데이트됩니다.
해당 데이터는 현재 mapStateToProps
을 통해 사용 가능해야합니까?
사용자가 작업을 수행했지만 초기 렌더링시 사용하지 않은 후에 어떻게 데이터를 사용할 수 있습니까? AppContainer
이이 세 가지 구성 요소를 렌더링하고 있다면 분명히 예상대로 렌더링하고 기능하도록 소품을 전달할 것입니다. 라이프 사이클을 사용할 수있는 곳이라고 생각하고 있습니다. 사용 방법이나 사용법을 확신 할 수 없습니다. AppContainer
, SearcBar
, & CurrentWeather
에 대한 코드는 다음과 같습니다. CurrentWeather
& Forecast
은 거의 동일합니다 (API의 다른 끝점에서 다른 데이터를 제공함). 그래서 제공하지 않았습니다. 이 리팩터러를 사용하기로 결정하기 전에 제대로 작동한다는 것을 알고 있기 때문에 액션이나 리듀서도 제공하지 않았습니다. 이걸 풀기 위해 하나 이상의 컨테이너가 필요할지도 모릅니다. 모든 조언이나 방향은 크게 감사 드리며, 모두에게 감사하고 좋은 밤을 보내십시오.
** _weatherSearch
에 SearchBar
이 양식 요소이기 때문에 나는 event.preventDefault();
이 있습니다. 이걸 제공해야합니까? event
이 전달되는 것은 아니지만 term
이 아니라고 생각합니다.
onSubmit={event => getWeather(event.target.value)}
앱 컨테이너 :
이import React, { Component } from 'react';
import { connect } from 'react-redux';
import { fetchCurrentWeather, fetchForecast } from '../actions/actions';
import SearchBar from '../components/SearchBar';
import CurrentWeather from '../components/CurrentWeather';
class AppContainer extends Component {
_weatherSearch(term) {
event.preventDefault();
// Here is where we go to fetch weather data.
this.props.fetchCurrentWeather(term);
this.props.fetchForecast(term);
}
render() {
const getWeather = term => {this._weatherSearch(term);};
return (
<div className="application">
<SearchBar getWeather={getWeather}/>
<CurrentWeather />
</div>
);
}
}
const mapStateToProps = ({ current, forecast }) => {
return {
current,
forecast
}
}
export default connect(mapStateToProps,
{ fetchCurrentWeather, fetchForecast })(AppContainer);
SearchBar에 :
import React from 'react';
const SearchBar = ({ getWeather }) => {
return(
<form className='input-group' onSubmit={event => getWeather(event.target.value)}>
<input
className='form-control'
placeholder='Search a US City' />
<span className='input-group-btn'>
<button className='btn btn-secondary' type='submit'>Submit</button>
</span>
</form>
);
}
export default SearchBar;
CurrentWeather : * 참고 : 내가 가진 SearchBar
의 형태 요소에 아래와 같이 event
를 사용하고 있습니다 논리 또는 데이터 처리를 제거하지 않았으므로 아직 수정되지 않았습니다. CurrentWeather
빨간색은 아직 프리젠 테이션 전용 구성 요소입니다.
import React, {Component} from 'react';
import {connect} from 'react-redux';
import {unitConverter} from '../conversions/conversions_2.0';
export class CurrentWeather extends Component {
_renderCurrentWeather(cityData) {
const name = cityData.name;
const {temp, pressure, humidity} = cityData.main;
const {speed, deg} = cityData.wind;
const {sunrise, sunset} = cityData.sys;
return (
<tr key={name}>
<td>{unitConverter.toFarenheit(temp)} F</td>
<td>{unitConverter.toInchesHG(pressure)}"</td>
<td>{humidity}%</td>
<td>{unitConverter.toMPH(speed)}mph {unitConverter.toCardinal(deg)}</td>
</tr>
);
}
render() {
let currentWeatherData = [];
if (this.props.current) {
currentWeatherData = this.props.current.map(this._renderCurrentWeather);
}
return (
<table className="table table-reflow">
<thead>
<tr>
<th>Temperature</th>
<th>Pressure</th>
<th>Humidity</th>
<th>Wind</th>
</tr>
</thead>
<tbody>
{currentWeatherData}
</tbody>
</table>
);
}
}
function mapStateToProps({current}) {
return {current};
}
export default connect(mapStateToProps)(CurrentWeather);
이'AppWrapper'는 다음 표상 구성 요소에 대한 로직을 포함하고 그냥 '통과 렌더링되는 하위 구성 요소 아래 props' 않습니다 AppContainer에서? – rockchalkwushock
나는 논리를 어디에 두는 것이 우위라고 생각한다. 저는 여러분이 비동기 액션을 꺼내서'AppWrapper'에 넣고 액션의 "상태"를 소품으로 바꾸는 것을 제외하고는, 여러분이 이전에했던 것처럼 모든 것을 생각하고있었습니다. 요약하면 래퍼는 비동기 액션을 포함하고, 컨테이너는 로직을 포함하고, 컴포넌트는 jsx를 포함합니다. – omul