reactjs - 反应条件标签名称不起作用 - 所有字母都缩小了
问题描述
我正在尝试根据道具值有条件地呈现标签名称。例子
const SimpleTagName = `Ball${size === 'large' ? 'Large' : 'Small'}`;
return (<SimpleTagName />
但问题是我得到了所有小写字母的“balllarge”标签。我做错了什么?
解决方案
试试这个方法:
import React from 'react';
import { PhotoStory, VideoStory } from './stories';
const components = {
photo: PhotoStory,
video: VideoStory
};
function Story(props) {
// Correct! JSX type can be a capitalized variable.
const SpecificStory = components[props.storyType];
return <SpecificStory story={props.story} />;
}
处理此模式的官方文档参考:https ://reactjs.org/docs/jsx-in-depth.html#choosing-the-type-at-runtime
推荐阅读
- excel - 将值粘贴到多列中的 VBA 代码
- reactjs - 如何通过使用 react 和 typescript 输入 url 来限制用户访问特定页面?
- c - 使用malloc初始化char *后如何获取char *指向的连续内存长度?
- php - 带有连接和值的 SQL 插入
- c# - 列表仅返回第一个值
- c# - C# 单元测试复杂的多线程数据库读取
- ethereum - web3 发送数据和令牌
- jquery - 如果(垂直)滚动条在另一个 div 中可见,则向 div 添加填充
- javascript - 如何使用 GET 方法向 URL 添加警报提示以从 URL 获取数据
- javascript - javascript - replace() 删除字符串中所有重复的字母