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

javascript-如何在React中传递带有事件处理函数的道具?【代码】

我正在尝试运行一个简单的应用程序,该应用程序以数组的形式呈现我一生获得的不同昵称. 我总共有两个组件,pushMe组件作为子按钮,nameFinder作为父按钮.我试图将我的display()方法作为onClick事件传递到pushMe按钮组件中,但该按钮不会呈现任何结果.我试图找出我所缺少的.import React from "react"; import ReactDOM from "react-dom"; import "./styles.css"; import { PushMe } from "../components/PushMe";class MyName extends ...

C#-ReactiveUI依赖注入构造函数【代码】

我在ReactiveUI中使用内置的依赖项注入器/ splat注入器. 我有要传递其适用的数据存储库的构造函数. 在其他框架中,它仅对接口使用反射,并使用GetServices调用来满足那些构造函数要求.例如,目前我具有用于创建类的此辅助程序扩展方法:/// <summary>/// Helper class for having a object's constructor automatically assigned by a "GetService" request./// </summary>/// <param name="resolver">The resolver.</param>/// <para...

javascript-ReactJS:this.props.data.map不是函数【代码】

我正在尝试从.json文件中获取数据,并通过视图显示结果.以下是scoreboard.json文件.{ "subject":"scoreboard", "copyright":"Copyright 2015", "data":"games":{"next_day_date":"2015-07-22","modified_date":"2015-08-04T07:34:50Z","month":"07","year":"2015","game":[{"game_type":"R","double_header_sw":"N","location":"Some City, State",}, {"game_type":"R","double_header_sw":"N","location":"Another City, Another S...

javascript-使用ES6在React中的多个子组件中传递函数【代码】

我有一个LayoutComponent,PageComponent和SingleComponent. 当我的用户单击按钮时,我想在NewPageComponent上向用户显示一条消息,我的应用程序将使用Meteor的FlowRouter将其路由到该消息. 为此,我将消息存储在LayoutComponent的状态中,然后将该消息和handlerMethod作为道具通过PageComponent传递给SingleComponent,后者是无状态功能组件. 我没有运气正确地将handlerMethod向下传递给SingleComponent以便在LayoutComponent上设置消息...

javascript-ReactDOMServer.renderToString不是函数【代码】

我在服务器上渲染react组件,并在路由时得到错误消息:const renderToString = ReactDOMServer.renderToStringconst fac = React.createFactory(React.createClass({render: function() { return (<Provider store={store}><StaticRouter location={location} context={routeContext}><App /></StaticRouter></Provider>)}}))const appHtml = renderToString(fac())解决方法:我建议你这样写:const ReactDOMServer = require('react...

javascript-在JSX中的嵌套地图函数中使用React JS打印数据【代码】

我正在尝试在嵌套地图函数中打印数据.由于某种原因,它不打印任何内容.这是我所拥有的:{dataFormat.protein_questions.map((item, index) => (<div key={_.uniqueId()} className="item"><div className="inline fields"><FieldonChange={handleChange}name={`protein_question[${index}].units_of_measurement`}component="select"className="ui dropdown2"required>{item.typing_methods.map((method, methodIndex) => {method.u...

javascript-React-TypeError:_this.setState不是函数【代码】

我正在玩React,并试图将用户输入的文本保存到状态输入中.我已经在文本区域添加了onChange属性以设置状态. 但是,当我开始键入内容时,我在控制台中看到指出TypeError的错误:_this.setState不是一个函数. 我尝试了各种修复方法,但是仍然没有.const NewItemForm = props => (<Form onSubmit={props.send_form}><Form.Group><TextArea placeholder='Name your first item here' name='item_msg'onChange={e => this.setState({ item_ms...

javascript-什么时候在REACT中使用构造函数合适?【代码】

我了解C之类的OOP语言中的构造函数的概念.但是,我不完全确定何时在REACT中使用构造函数.我确实了解JavaScript是面向对象的,但是我不确定构造器实际上是在“构造”什么. 呈现子组件时,子组件中是否需要构造函数?例如:class App extends React.Component {constructor(props) {super(props);this.state = {items: [],error: null}}render () {return (<React.Fragment><ChildComponent data={this.state.items}></ChildComponent><...

javascript-React Native setState不是函数【代码】

有人可以解释一下为什么this.setState不是函数吗? 我看不到为什么我的代码有错误import React from 'react'; import axios from 'axios' import { StyleSheet, Text, View , Image} from 'react-native';export default class App extends React.Component {constructor(){super();this.state = {res: []}}componentDidMount() {axios.get('https://api.github.com/repos/torvalds/linux/commits').then(function (response) {thi...

javascript – 为什么在构造函数之外设置React Component的状态?【代码】

所以我刚刚从React框架下载了源代码,我在终端中收到了这个错误:ERROR in ./src/components/TextEditor.jsModule build failed: SyntaxError: Unexpected token (24:8)22 | 23 | // Set the initial state when the app is first constructed. > 24 | state = {| ^25 | state: initialState26 | }27 | 我的问题是,为什么人们会像这样设置React Component的状态?如果某些人的错误会有什么好处?另外,我可以使用B...

javascript – 通过reactjs上的props传递带参数的函数【代码】

我有一个函数,它从父级一直到组件层次结构中的子级的子级.通常这不会是一个太大的问题,但我需要从孩子那里收到一个参数.我目前收到此错误消息:Uncaught(在promise中)TypeError:this.props.myFunction不是函数. 这是我正在做的一个示例代码:class SomeComponent extends Component{constructor(props){super(props);//does whatever stuff this.myFunction = this.myFunction.bind(this);}//(only applicable to raw and ...

javascript – 从React Native中的数组映射函数动态渲染内容【代码】

我正在尝试从数组中获取数据并使用map函数来呈现内容.看着**{this.lapsList()}** 和相关的**lapsList()** 功能,以了解我正在尝试做什么.结果是什么都没有显示(视图下的视图等)这是我的简化代码:class StopWatch extends Component {constructor(props) {super(props);this.state = {laps: []}; }render() {return (<View style={styles.container}><View style={styles.footer}><View><Text>coucou test</Text></View>{this.lapsL...

javascript – 在.map函数中使用react道具[复制]【代码】

参见英文答案 > How to access the correct `this` inside a callback? 10个假设我的一个组件上有以下渲染功能.从父元素我传递了一个changeTid prop函数. 家长:<RequestsList data={this.state.data} changeTid={this.changeTid} />儿童: (我正在使用ES6课程)render() { var RequestNodes = this.props.data.map(function(request) {return (<Request key={request.TID} changeTid={this.p...

javascript – 如何使用React useEffect仅调用一次加载函数【代码】

useEffect React挂钩将在每次更改时运行传入函数.这可以进行优化,只有在所需的属性发生变化时才能调用它. 如果我想从componentDidMount调用初始化函数而不是在更改时再次调用它,该怎么办?假设我想加载一个实体,但加载函数不需要组件中的任何数据.我们如何使用useEffect钩子来做这个?class MyComponent extends React.PureComponent {componentDidMount() {loadDataOnlyOnce();}render() { ... } }使用钩子,这可能是这样的:funct...

javascript – React Hook使用中的异步函数警告效果:useEffect函数必须返回一个清理函数或者什么也没有【代码】

我正在尝试下面的useEffect示例:useEffect(async () => {try {const response = await fetch(`https://www.reddit.com/r/${subreddit}.json`);const json = await response.json();setPosts(json.data.children.map(it => it.data));} catch (e) {console.error(e);} }, []); 我在我的控制台中收到此警告.但我认为清理对于异步调用是可选的.我不知道为什么我会收到这个警告.链接沙箱的例子. https://codesandbox.io/s/24rj871r0p解...