GVKun编程网logo

reactjs中的render和class内部的函数(react render函数)

14

针对reactjs中的render和class内部的函数和reactrender函数这两个问题,本篇文章进行了详细的解答,同时本文还将给你拓展classList.add仅在ReactJs中首次工作、c

针对reactjs中的render和class内部的函数react render函数这两个问题,本篇文章进行了详细的解答,同时本文还将给你拓展classList.add仅在ReactJs中首次工作、className react.js内部的调用函数、javascript – 如何在reactjs的render方法中使用三元运算符?、React-如何调用另一个回调函数内部的函数等相关知识,希望可以帮助到你。

本文目录一览:

reactjs中的render和class内部的函数(react render函数)

reactjs中的render和class内部的函数(react render函数)

我正在尝试学习reactjs并且有一些不确定性。我指的是react
DOCS和其他一些教程,我看到函数是在render函数和类内部编写的。我们应该在render函数内部做些什么反应?

第一种方式

class App extends Component {    test(user) {        return user.firstName;    }    render() {        const user = {            firstName: ''Harper'',            lastName: ''Perez''        };        return (            <div>                <h1>{this.test(user)}</h1>            </div>        )    }}

第二路

class App extends Component {       render() {        const user = {            firstName: ''Harper'',            lastName: ''Perez''        };        function test(user) {            return user.firstName;        }        return (            <div>                <h1>{test(user)}</h1>            </div>        )    }}

这两种方法都有效。但是我想知道什么是最好的方法吗?最重要的是,我想知道我可以在render函数中做什么事情。

谢谢。

答案1

小编典典

我认为这最终是您的选择,但我个人更喜欢仅将功能放在render中,这些功能专门处理渲染组件和/或JSX(即,在prop上进行映射,根据prop来有条件地加载适当组件的switch语句等)。
)。如果该函数背后的逻辑很沉重,我将使其不渲染。

这是一个例子。假设您的组件中有一个“用户”道具,该道具应该显示用户列表。您可能具有以下类型的事物的render函数:

render(){  // An array of user objects & a status string.  const { users, status } = this.props;  // Map users array to render your children:  const renderUserList = () => {    return users.map(user => {      return <div>{ user.firstName }</div>;    });  };  // Conditionally load a component:  const renderStatus = () => {    let component = '''';    switch(status){      case ''loading'':        component = <Component1 />        break;      case ''error'':        component = <Component2 />        break;      case ''success'':        component = <Component3 />        break;      default:        break;    }    return component;  }  // render() return:  return(    <div>      <div className="status">        { renderStatus() }      </div>      <div className="user-list">        { renderUserList() }      </div>    </div>  );}

但是,如果您有一个需要以某种方式操纵有关用户数据的函数,则最好将其放在render之外的函数中。

classList.add仅在ReactJs中首次工作

classList.add仅在ReactJs中首次工作

如果您的浏览器堆栈允许,则可以使用:focus-within伪类来实现相同的行为。

您只需要li:focus-within { outline: 2px solid blue }

而不是一堂课

className react.js内部的调用函数

className react.js内部的调用函数

在本例中,如何getClassclassName内部调用函数?我写出来的方式似乎没有用getClass

var CreateList = React.createClass({    getClass: function() {        //some code to return className    },    render: function() {        return(            <div className{this.getClass}>Example</div>        );    }});

答案1

小编典典

您引用的是getClass()函数实例,而不是调用函数。尝试像这样调整它:

render: function() {    return(        <div className={this.getClass()}>Example</div>    );}

javascript – 如何在reactjs的render方法中使用三元运算符?

javascript – 如何在reactjs的render方法中使用三元运算符?

我想使用三元运算符基于某些状态条件渲染两个按钮,以避免代码重复.

我想做什么?

我有两个按钮取消和启动基于状态值load_cancel.如果单击取消按钮,则单击load_cancel设置为true,当load_cancel设置为false时,将显示“开始”按钮.所以我在render方法中有这样的东西

{props.item_id && this.props.load_cancel &&
    <button onClick= 
        {this.props.handle_load_start}>start</button>}
{props.item_id && !this.props.load_cancel  &&
    <button onClick={this.props.handle_load_cancel}>Cancel</button>}

如何使用三元运算符执行相同的操作?

谢谢.

解决方法

像这样的东西

prop.item_id ? (
  this.props.load_cancel ? (
    <button onClick={this.props.handle_load_start}>start</button>
  ) : (
    <button onClick={this.props.handle_load_cancel}>Cancel</button>
  )
) : null

React-如何调用另一个回调函数内部的函数

React-如何调用另一个回调函数内部的函数

您可以简单地在expandUnexpandSchema上下文之外定义loadSchemaInfo函数。我不完全知道您的代码的逻辑,但这是一个可能的解决方案:

function useSchemaState() {

    const [expand,setExpand] = useKeyState('expand',false);

    const expandSchema = useCallback(() => {
        setExpand((expand) => !expand);
        expandUnexpandSchema(data); // take data from somewhere.. I suppose you have it
    },[setExpand]);

    const expandUnexpandSchema = (data) =>
    {
        const expanded = {};

        Object.keys(data).forEach((schemaName) => {
            expanded[schemaName] = expand;
        });

        return expanded;
    }

    const loadSchemaInfo = useCallback(
        async(connectionId,reload) => {

            // Some more lines of code

            // do something with expanded
            const expanded = expandUnexpandSchema(data);
        }
    );
};
,

在loadSchemaInfo之外定义它,并在loadSchemaInfo和expandSchema中调用它,或尝试以下操作:

const expandSchema = useCallback(() => {
    setExpand((expand) => !expand);
    loadSchemaInfo.expandUnexpandSchema();
},[setExpand]);

const loadSchemaInfo = useCallback(
    async(connectionId,reload) => {

        // Some more lines of code
        const expanded = {};

        expandUnexpandSchema() {
            if (data) {
                Object.keys(data).forEach((schemaName) => {
                    expanded[schemaName] = expand;
                });

            };
        }
       loadSchemaInfo.expandUnexpandSchema = expandUnexpandSchema;
    );

};

基于Javascript call nested function

在功能方面,花括号之间的任何内容都无法通过外部访问,除非可以通过点符号属性分配或return语句进行访问。您可以在外部函数的外部或内部进行赋值,但是无论哪种方式,都需要外部动作,最少要运行一次外部函数。 – Magnus Lind Oxlund 19年6月2日在12:21

今天关于reactjs中的render和class内部的函数react render函数的介绍到此结束,谢谢您的阅读,有关classList.add仅在ReactJs中首次工作、className react.js内部的调用函数、javascript – 如何在reactjs的render方法中使用三元运算符?、React-如何调用另一个回调函数内部的函数等更多相关知识的信息可以在本站进行查询。

本文标签: