【React生命周期函数】教程文章相关的互联网学习教程文章

javascript – React委托正常事件处理程序到文档,绑定函数怎么样?【代码】

如您所知,React会自动将所有事件委托给文档,就像这里的大列表一样:handleTodo(){} render() { todos.map((todo)=><li onClick={this.handleTodo}>{todo.name}</li>)}将生成待办事项列表,但onClick事件将委托给文档,对吧? 但问题是关于绑定功能.handleTodo(todo){/**update todo */} render(){todos.map((todo)=><li onClick={this.handleTodo.bind(this,todo)}>{todo.name}</li>)}对于每个待办事项,由于参数不同,因此存在不同的绑...

javascript – 在react组件中写一个类之外的函数【代码】

我见过这样的代码function abc(){return 'abc' } class MyComponent extends React.Component {static abc = abc;render() { return <h1>{this.abc}</h1>; } }其中函数abc在react类之外定义.我不知道为什么作者这样做,为什么不能在课堂上这样做呢?解决方法:这些是ES6 static methods,并不是React独有的.它们是组件类的成员,而不是组件的实例.它们并没有在React中广泛使用,但它们可能很有用.它甚至在React docs中提到:Sometimes i...

javascript – React .map()不是函数错误【代码】

import React, {Component} from 'react'; import './App.css'; import axios from 'axios';export default class App extends Component {constructor() {super();this.state = {weather: []};} componentDidMount (){this.search(); }search = () => {axios.get("http://api.openweathermap.org/data/2.5/weather?q=Houston&APPID=f2327cca8f3d665f4c9f73b615b294ed").then(res => {this.setState({weather: res.data});}).catch...

javascript – 将props传递给withRouter()函数中包含的react组件【代码】

我正在使用React-Router v4在我的React应用程序中导航.以下是包含在withRouter()函数中的组件,以使其能够在单击时更改路由:const LogoName = withRouter(({history, props}) => (<h1{...props}onClick={() => {history.push('/')}}>BandMate</h1> ));正如您所看到的,我将props传递给组件,我需要它来更改组件的类.这里的问题是在< LogoName>中未定义道具.零件.当我点击另一个组件时,我需要能够更改此组件的类,如下所示:<LogoName ...

javascript – 构造函数被调用两次React Component【代码】

我的反应组件的构造函数被调用两次,但我无法弄清楚原因.我使用react-redux来存储我的应用程序的语言.我有一个函数,它根据浏览器的语言设置默认语言. LoginPage是第一个获取渲染的组件,因此我在其构造函数中调用了我的函数.基本上它的作用是比较和发送一个动作.当我使用redux开发人员工具检查我的状态时,我发现它已经被派遣了两次.我在构造函数中打印了虚拟数据,它也被打印了两次. LoginPage.jsimport React from 'react'; import {...

javascript – 在NavigatorIOS中调用onRightButtonPress的函数 – React Native【代码】

我在一个反应??原生的NavigatorIOS中使用onRightButton.我希望能够调用一个驻留在我正在推动的组件中的函数,但我无法知道如何实现它.这是一个代码示例:this.props.navigator.push({component: SingleResultView,title: "SomeTitle",rightButtonIcon: require('image!mapsmarker'),passProps: {userPosition: this.props.userPosition},onRightButtonPress: () => { SingleResultView.foo(); } // NOT WORKING! });我怎样才能做到这...

javascript – 我什么时候需要使用super(props)将prop传递给react组件的构造函数?【代码】

参见英文答案 > What’s the difference between “super()” and “super(props)” in React when using es6 classes? 10个很多时候我们在构造函数中发送道具但我们从不在构造函数中的任何地方使用this.props,所以为什么需要传递它以及何时需要这样做.class App extends React.Component {constructor(props) {super(props); // When do we need to send props to the constructorthis.sta...

javascript – 为什么我能够使用this.state而不必绑定或使用箭头函数React【代码】

我知道箭头函数继承了父元素的上下文,这就是它们在React中非常有用的原因.但是,我有这个React组件:import React, { Component } from 'react'; import { View, Text } from 'react-native'; import axios from 'axios';class AlbumList extends Component {constructor(props) {super(props);this.state = {albums: [],};axios.get('https://rallycoding.herokuapp.com/api/music_albums').then(response => {this.setState({ alb...

javascript – TypeError:fetch不是React Native&Jest中的函数【代码】

我有一个React Native项目,它只包含使用新项目创建的示例Jest测试(他们只检查渲染是否适用于iOS和Android). 我的项目使用其中一个组件中的网络请求获取.这在我运行应用程序时工作正常,但是当我运行测试时,它们会失败并显示以下错误消息:TypeError: fetch is not a function.谁知道发生了什么? 这是我的代码中使用fetch的部分:export default class MainScene extends Component {constructor(props) {super(props);this.renderR...

React 函数式组件性能优化

一 函数式组件新能优化:https://mp.weixin.qq.com/s?__biz=MzI1ODk2Mjk0Nw==&mid=2247484774&idx=1&sn=9dc58e54a28755504d58bef49a78f3b4&scene=21#wechat_redirect React 性能优化的理念的主要方向就是这两个:减少重新 render 的次数。因为在 React 里最重(花时间最长)的一块就是 reconction(简单的可以理解为 diff),如果不 render,就不会 reconction。减少计算的量。主要是减少重复计算,对于函数式组件来说,每次 render 都...

react(57)——对象型setState和函数型setState【代码】

1.对象型setState add = () => {let { count } = this.state;count += 1;this.setState({ count },()=>{console.log(count);}); };对象型setState的参数后面可以接上一个回调函数,当setState执行的时候,会调用后面这个回调函数。 2.函数型setState add = () => {this.setState((state, props) => {return { count: state.count + 1 };}); };使用函数型setState可以在里面传入一个回调函数。回调函数可以获取当前组建的state和pro...