- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在开发一个应用程序,它将帮助我使用 openweathermap.org 获取任何城市的五天天气预报,但是,每当我在 WeatherList 容器中调用函数 renderWeather 时,我都会在浏览器中收到上述错误,如图所示在我下面的代码片段中。
这个文件是我导入redux Promise的index.js
import React from 'react';
import ReactDOM from 'react-dom';
import { Provider } from 'react-redux';
import { createStore, applyMiddleware } from 'redux';
import ReduxPromise from 'redux-promise';
import App from './components/app';
import reducers from './reducers';
const createStoreWithMiddleware = applyMiddleware(ReduxPromise)(createStore);
ReactDOM.render(
<Provider store={createStoreWithMiddleware(reducers)}>
<App />
</Provider>
, document.querySelector('.container'));
weather_list 容器的代码如下
import React, {Component} from 'react';
import {connect} from 'react-redux';
class WeatherList extends Component{
renderWeather(cityData){
return(
<tr>
<td>{cityData.city.name}</td>
</tr>
);
}
render(){
return(
<table className="table table-hover">
<thead>
<tr>
<th>City</th>
<th>Temperature</th>
<th>Pressure</th>
<th>Humidity</th>
</tr>
</thead>
<tbody>
{this.props.weather.map(this.renderWeather)}
</tbody>
</table>
);
}
}
function mapStateToProps(state){
return {
weather: state.weather
};
}
export default connect(mapStateToProps)(WeatherList);
search_bar 容器的代码如下
import React, {Component}from 'react';
import {connect} from 'react-redux';
import {bindActionCreators} from 'redux';
import {fetchWeather} from '../actions/index';
class SearchBar extends Component{
constructor(props){
super(props);
this.state = {term: ''};
this.onInputChange = this.onInputChange.bind(this);
this.onFormSubmit = this.onFormSubmit.bind(this);
}
onInputChange(event){
console.log(event.target.value);
this.setState({term: event.target.value});
}
onFormSubmit(event){
event.preventDefault();
// we need to go and fetch weather data!
this.props.fetchWeather(this.state.term);
this.setState({term: ''});
}
render(){
return(
<form onSubmit={this.onFormSubmit} className="input-group">
<input
placeholder="Get a five-day forecast in your favorite cities"
className="form-control"
value={this.state.term}
onChange={this.onInputChange} />
<span className="input-group-btn">
<button type="submit" className="btn btn-secondary">Submit</button>
</span>
</form>
);
}
}
function mapDispatchToProps(dispatch){
return bindActionCreators({fetchWeather:fetchWeather}, dispatch)
}
export default connect(null, mapDispatchToProps)(SearchBar);
应用程序组件的代码如下所示
import React, { Component } from 'react';
import SearchBar from '../containers/search_bar.js';
import WeatherList from '../containers/weather_list.js';
export default class App extends Component {
render() {
return (
<div>
<SearchBar />
<WeatherList />
</div>
);
}
}
天气减少器的代码如下所示
import {FETCH_WEATHER} from '../actions/index';
export default function(state = [], action){
switch (action.type) {
case FETCH_WEATHER:
//return state.concat([action.payload.data]);
return ([action.payload.data, ...state])
}
return state;
}
reducers文件夹中index.js的代码如下所示
import { combineReducers } from 'redux';
import WeatherReducer from './reducer_weather';
const rootReducer = combineReducers({
weather: WeatherReducer
});
export default rootReducer;
操作文件夹中操作创建者的代码如下所示
import axios from 'axios';
const API_KEY = '03d17752ca362bc60ca7df94aac228a6';
const ROOT_URL =`https://api.openweathermap.org/data/2.5/forecast?appid=${API_KEY}`;
export const FETCH_WEATHER = 'FETCH_WEATHER';
export function fetchWeather(city){
const url = `${ROOT_URL}&q=${city},us`;
const request = axios.get(url);
console.log('Request:', request);
return{
type: FETCH_WEATHER,
payload: request
}
}
最后,下面是显示浏览器中错误的图像。 enter image description here
非常感谢有关如何解决该错误的任何帮助
最佳答案
最初 this.props.weather 将是未定义的,因此您需要在执行 .map 之前进行条件检查,就像以两种方式进行
this.props.weather && this.props.weather.map
或者
Array.isArray(this.props.weather) && this.props.weather.map
此外,在执行 .map 时,您需要将天气数据传递给 renderWeather 方法,因为它需要数据,但您没有在 .map 中将数据传递给它
改变
{this.props.weather.map(this.renderWeather)}
至
{Array.isArray(this.props.weather) && this.props.weather.map(item => this.renderWeather(item))}
同时在此处为 tr 元素设置唯一键
renderWeather(cityData){
return(
<tr key={cityData.city && cityData.city.id}> //if you do not have unique id then use index as key
<td>{cityData.city && cityData.city.name}</td>
</tr>
);
}
或者如果您的数据不包含唯一 ID,则使用索引作为键
{Array.isArray(this.props.weather) && this.props.weather.map((item, index) => this.renderWeather(item, index))}
同时在此处为 tr 元素设置唯一键
renderWeather(cityData, index){
return(
<tr key={"city-"+index}> //if you do not have unique id then use index as key
<td>{cityData.city && cityData.city.name}</td>
</tr>
);
}
关于javascript - TypeError : this. props.weather 未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53129700/
我仍在学习如何将 API 数据与 react 和 nextjs 一起使用。但是,为什么我的函数只在我编写 {props.props.title} 而不是我期望的 {props.title} 时起作用?
我仍在学习如何将 API 数据与 react 和 nextjs 一起使用。但是,为什么我的函数只在我编写 {props.props.title} 而不是我期望的 {props.title} 时起作用?
我正在用 TypeScript 构建一个 React 应用程序。我有一个 RequiresPermission基于谓词的组件应该渲染一个或另一个组件并转发所有 Prop 。 type Props =
我想通过 gatsby 布局传递我所有的 props。例如: import React, { Component } from 'react'; export default class Exampl
如果我使用合成属性,那我为什么不直接说: self.property = nil; 这将释放引用计数,并确保我没有悬挂指针。 看起来很简单,但我看到的 99% 的代码似乎都是这样做的: [proper
Eslint 抛出 eslint(react/prop-types) 错误,尽管已经声明了 propTypes。我正在使用 eslint-plugin-react 我研究了其他几个类似的问题以及 li
我正在使用以下由 linter eslint-plugin-react 解析的代码。它返回警告: "product is missing in props validation" 当我在底部的 pro
我正在尝试在 React 应用程序中添加 TypeScript。 版本: "react": "16.9.0", "typescript": "3.5.3", 我有一个像这样的数组 import aLo
我有一个组件 . 如果组件没有 this.props.children , 我想设置 Prop ariaLabel作为isRequired ,否则 in 可以是可选的。我该怎么做? ariaLabe
我应该用一个代替另一个吗?一起使用它们更好吗?谢谢。 最佳答案 prop in obj 检查 obj 是否有名为 prop 的属性,即使它只是从原型(prototype)继承而来。 obj.hasOw
我的组件 Text有 2 个 Prop :isHideable: boolean和 hidden: boolean .我如何允许 Hidden仅在 isHideable 时作为 Prop 是true
我试图将带有一些 Prop 的功能组件发送到另一个组件,并在接收组件中尝试键入检查该组件的某些 Prop 。这是代码: // BaseButton.tsx export type ButtonProp
是否可以从也作为 prop 传递的未知组件推断出正确的 props 类型? 如果已知组件(存在于当前文件中),我可以获得 Prop : type ButtonProps = React.Compone
我对 react 还很陌生,这是我正在努力解决的问题。 有一个父组件 家长 它将 Prop 传递给 child 。 其中一个 Prop ,包括一个要渲染的元素,如下所示: 在子组件中,我想获取这个组
我想做一个 Tabs推断 active 的可能值的组件prop 基于它的 child 拥有的东西 name Prop 。这就是我尝试这样做的方式: import React from 'react'
我对 react 还很陌生,并且只有当用户开始向下滚动时,我才尝试将更多信息加载到记录数组中。问题是新信息出现在数组中,但如果您尝试调用它,它将返回undefined。我在这里不明白什么? 父组件:
因此,如果我对一个组件有很多不同的 Prop ,我希望我可以做类似的事情 const { ...props } = props; 而不是 const { prop1, prop2, prop3, ..
这是我寻求指导的问题类型,因为我不确定我正在寻找的内容是否存在...... 上下文:我正在使用 Firestore 来保存数据,并且正在构建一个可重用的自定义 Hook (React 16.8),以便
我有一个 React 组件,它获取一个配置对象作为 prop,它看起来像这样: { active: true, foo: { bar: 'baz' } } 在
如何附加另一个属性?我有一个 react 组件,其中有人传入 ...props,我想附加一个额外的 Prop 最佳答案 请记住,传递 Prop 的顺序将决定将哪个值传递给该组件。这适用于有两个同名
我是一名优秀的程序员,十分优秀!